2017-06-09 11 views
2

SpringBootはSpring RestとJacksonで使用します。 Java 8 LocalDateTimeを使用しています。春休みxml dateTime形式

RestController。コントローラによって返される

@RestController 
@RequestMapping(method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE}) 
public class SimpleRestController { 

    @Autowired 
    private RestService restService; 

    @RequestMapping("/api/{id}") 
    public ResponseEntity<RestObject> getModel(@PathVariable Long id) { 
     RestObject restObject = restService.getModel(id); 
     HttpStatus httpStatus = HttpStatus.OK; 

     if (restObject == null) { 
      httpStatus = HttpStatus.NO_CONTENT; 
     } 

     return new ResponseEntity<>(restObject, httpStatus); 
    } 
} 

RestObject

import javax.xml.bind.annotation.XmlRootElement; 
import java.io.Serializable; 
import java.time.LocalDateTime; 

@XmlRootElement 
public class RestObject implements Serializable { 

    private LocalDateTime timestamp; 
    private String title; 
    private String fullText; 
    private Long id; 
    private Double value; 

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") 
    public LocalDateTime getTimestamp() { 
     return timestamp; 
    } 

    //Other getters and setters. 
} 

Accept=application/jsonヘッダーでGETリクエストを送信するとうまく動作します。これがレスポンスです。しかしAccept=application/xml

{ 
    "timestamp": "2017-06-09 15:58:32", 
    "title": "Rest object", 
    "fullText": "This is the full text. ID: 10", 
    "id": 10, 
    "value": 0.22816149915219197 
} 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<restObject> 
    <fullText>This is the full text. ID: 10</fullText> 
    <id>10</id> 
    <timestamp/> 
    <title>Rest object</title> 
    <value>0.15697306201038086</value> 
</restObject> 

タイムスタンプフィールドは空です。それを動作させるには?

+0

のLocalDateTimeは、Java 8またはjodatimeからであるからアイデアを取りましたか? – pvpkiran

+0

Java 8 - java.time –

+0

Java 8 LocalDateTimeはサポートされていません。 Joda LocalDateTimeを使用するか、コンバータを作成してください。これは役立つかもしれないhttps://stackoverflow.com/questions/29424551/java-unmarshall-localdatetime-error – pvpkiran

答えて

2

ここに解決策があります。

これは、応答

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<restObject> 
    <fullText>This is the full text. ID: 10</fullText> 
    <id>10</id> 
    <timestamp>2017-06-09 16:31:01</timestamp> 
    <title>Rest object</title> 
    <value>0.0021564103099468435</value> 
</restObject> 

1である)クラスDateTimeAdapter

import javax.xml.bind.annotation.adapters.XmlAdapter; 
import java.time.LocalDateTime; 

public class DateTimeAdapter extends XmlAdapter<String, LocalDateTime> { 

    public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; 
    public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(DATE_FORMAT); 

    public LocalDateTime unmarshal(String v) throws Exception { 
     return LocalDateTime.parse(v, DATE_TIME_FORMATTER); 
    } 

    public String marshal(LocalDateTime v) throws Exception { 
     return DATE_TIME_FORMATTER.format(v); 
    } 
} 

2)RestObjectクラスを更新します。 LocalDateTimeフィールドに@ XmlJavaTypeAdapter(DateTimeAdapter.class)アノテーションを追加します。

import com.ca.training.rest.server.config.DateTimeAdapter; 
import com.fasterxml.jackson.annotation.JsonFormat; 

import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 
import java.io.Serializable; 
import java.time.LocalDateTime; 

import static com.ca.training.rest.server.config.DateTimeAdapter.DATE_FORMAT; 

@XmlRootElement 
public class RestObject implements Serializable { 

    private LocalDateTime timestamp; 
    private String title; 
    private String fullText; 
    private Long id; 
    private Double value; 

    @XmlJavaTypeAdapter(DateTimeAdapter.class) 
    @JsonFormat(pattern = DATE_FORMAT) 
    public LocalDateTime getTimestamp() { 
     return timestamp; 
    } 

    //Other getters and setters. 
} 

は、私がここに http://blog.bdoughan.com/2011/05/jaxb-and-joda-time-dates-and-times.html 、ここ JAXB: Isn't it possible to use an XmlAdapter without @XmlJavaTypeAdapter?