첫 번째 사항 먼저 : joda DateTime에 MessageBodyReader/Writer
이 필요합니다.
일부 조정을하지 않으면 JSON 직렬화 된 DateTime의 POST가 작동하지 않습니다. >iChronology
기본 비 - 인수 생성자, GSON 필요가 없습니다 - 당신이 GSON와 함께 다시 역 직렬화 할 경우, GSON이 실패합니다
{ "iMillis": 1414507195233,
"iChronology": {
"iBase": {
"iBase": {
...
},
"iParam": {
...
}}}}
org.joda.time.Chronology
가 있기 때문에, :하는처럼 GSON 직렬화 된 날짜 시간 모양 객체를 직렬화 복원합니다. Afaig, 표준 디시리얼라이저를 사용하여 DateTime을 비 직렬화하는 동안 이러한 문제가 발생합니다.
그래서 나는
FasterXML/jackson-datatype-joda
리더를 기반으로 MessageBodyReader/라이터을 만드는 결국했습니다
// ...
import org.joda.time.DateTime;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.joda.JodaModule;
@Consumes(MediaType.APPLICATION_JSON)
public class JodaTimeBodyReader implements MessageBodyReader<DateTime> {
private static ObjectMapper mapper = new ObjectMapper();
public JodaTimeBodyReader() {
mapper.registerModule(new JodaModule());
}
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return type == DateTime.class;
}
@Override
public DateTime readFrom(Class<DateTime> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
try {
return mapper.readValue(entityStream, DateTime.class);
} catch (Exception e) {
throw new ProcessingException("Error deserializing a org.joda.time.DateTime.", e);
}
}
}
저자 : 모두 ResourceConfig에 등록
// ...
import org.joda.time.DateTime;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.joda.JodaModule;
@Provider
@Consumes(MediaType.APPLICATION_JSON)
public class JodaTimeBodyWriter implements MessageBodyWriter<DateTime> {
private static ObjectMapper mapper = new ObjectMapper();
public JodaTimeBodyWriter() {
mapper.registerModule(new JodaModule());
}
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return type == DateTime.class;
}
@Override
public long getSize(DateTime t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
// deprecated by JAX-RS 2.0 and ignored by Jersey runtime
return 0;
}
@Override
public void writeTo(DateTime t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
try {
entityStream.write(mapper.writeValueAsBytes(t));
} catch (Exception e) {
throw new ProcessingException("Error serializing a org.joda.time.DateTime to the output stream", e);
}
}
}
.
Writer는 1414507195233 (yap, JSON이 아님)으로 응답하고 리소스를 POST로 돌려 보내면 올바른 DateTime을 받게됩니다.
위 예제와 같이 JSON을 얻으면 Reader를 업그레이드하여 iMillis 용 JSON을 구문 분석하고 Long 값을 사용할 수 있습니다. TimeZone의 경우 동일한 작업을 수행하고 dateTime.withZone(...)
을 사용하여 값을 설정할 수 있습니다. 내가 사용했던
메이븐 의존성 :
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>2.4.0</version>
</dependency>
뉴저지 2.12
희망이 어떻게 든 도움이되었다.
@POST
@Path("/{memberId}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public ResultBean recordDate(@PathParam("memberId") Long memberId, DateTime dateTime) {
// TODO stuff happens
return new ResultBean(memberId, dateTime);
}
당신은 할 수 : 대신
무엇이 오류입니까? 스택 추적을 보여 주시겠습니까? –