2017-05-02 1 views
2

객체에 @DateTimeFormat 이라는 주석을 달았습니다. 왜 인식하지 못합니까?@DateTimeFormat을 인식하지 못했습니다.

{ 
    "timestamp": 1493708443198, 
    "status": 400, 
    "error": "Bad Request", 
    "exception": "org.springframework.http.converter.HttpMessageNotReadableException", 
    "message": "Could not read JSON document: Can not construct instance of java.time.LocalDateTime: no String-argument constructor/factory method to deserialize from String value ('2015-09-26T01:30:00.000')\n at [Source: [email protected]; line: 5, column: 23] (through reference chain: net.petrikainulainen.spring.trenches.model.Topic[\"localDateTime\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.time.LocalDateTime: no String-argument constructor/factory method to deserialize from String value ('2015-09-26T01:30:00.000')\n at [Source: [email protected]; line: 5, column: 23] (through reference chain: net.petrikainulainen.spring.trenches.model.Topic[\"localDateTime\"])", 
    "path": "/api/topics" 
} 

:

내 주요 아이디어는 문자열이 컨트롤러로 수신되면, 그것은 내가 갖는 순간 LocalDateTime 객체

enter image description here

로 변환하는 것입니다 게시하려고합니다

{ 
    "id": "javaw2", 
    "name": "java code", 
    "descript2ion": "java description", 
    "localDateTime": "2015-09-26T01:30:00.000" 
} 

이것은 m입니다. Y 컨트롤러 :

@RequestMapping(method = RequestMethod.POST, value = "/topics") 
public void addTopic(@RequestBody Topic topic) { 
    topicService.addTopic(topic); 
} 

답변

2

당신은 잭슨 인식 할 수있는 형식으로 이미로 @DateTimeFormat으로 필드에 주석을 할 필요가 없습니다. 문자열을 LocalDateTime으로 역 직렬화 할 수 있도록 ObjectMapper 구성에 JavaTimeModule을 추가하기 만하면됩니다.

모델 :

class Model { 
    private LocalDateTime date; 

    public LocalDateTime getDate() { 
     return date; 
    } 

    public void setDate(LocalDateTime date) { 
     this.date = date; 
    } 
} 

역 직렬화 :

public static void main(String[] args) throws Exception { 
    String json = "{\"date\" : \"2015-09-26T01:30:00.000\"}"; 
    ObjectMapper mapper = new ObjectMapper(); 
    mapper.registerModule(new JavaTimeModule()); 

    Model model = mapper.readValue(json, Model.class); 
    System.out.println(model.getDate()); 
} 

이 작업을 수행하려면, 위 잭슨 버전 2.8.5 이상을 사용해야합니다, here's 문서 다음은 예입니다 .

3

java.time.LocalDateTime의 인스턴스를 생성 할 수 없음 : 어떤 문자열 인수 생성자/팩토리 메소드를 문자열 값에서 역 직렬화 ('2015-09-26T01 : 30 : 00.000')

오류 LocalDateTime 클래스 String 인수 생성자/팩터 리 메서드가 없다는 것을 의미하므로 디시리얼라이저Date 문자열 표현을 LocalDateTime Object로 역 직렬화해야합니다. 같은

뭔가 :

@JsonDeserialize(using = MyDateDeserializer.class) 
private LocalDateTime localDateTime; 

다음 MyDateDeserializer 구현

public class MyDateDeserializer extends JsonDeserializer<LocalDateTime> { 
    @Override 
    public LocalDateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws Exception { 

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("your pattern"); 

    String date = jp.getValueAsString(); 

    LocalDateTime localDateTime = LocalDateTime.parse(date, formatter); 
    return localDateTime; 
    } 
} 
+0

는 설명 주셔서 감사합니다. 그것은 매우 도움이되었다 –

+0

고마워 친구;) –

관련 문제