입력 json이 유효하지 않은 경우를 대비해 json 매핑 예외를 잡기를 원합니다.저지/잭슨 : json 매핑 예외를 잡는 방법?
그것은 org.codehaus.jackson.map.JsonMappingException
을 던졌습니다. 그러나 나는이 예외를 잡는 방법이나 장소를 알지 못합니다. 이 예외를 잡아 적절한 오류 응답을 다시 보내고 싶습니다.
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
"name",
"id"
})
public class Customer {
@JsonProperty("name")
private String name;
@JsonProperty("id")
private String id;
<setter/getter code>
}
public class MyService {
@POST
@Consumes(MediaType.APPLICATION_JSON)
public final Response createCustomer(@Context HttpHeaders headers,
Customer customer) {
System.out.println("Customer data: " + customer.toString());
return Response.ok("customer created").build();
}
}
모든 것이 잘 작동하지만 JSON 본체 그럼 형성되지 않는 경우는 JsonMappingException
예외가 발생합니다. 이 예외를 catch하고 싶습니다. 나를 JsonMappingException
에 대한 ExceptionMapper
제공자를 선언했습니다 위해 마지막으로 일을 무엇
항목 : https://java.net/jira/browse/JERSEY-2722 –