Tôi muốn bắt ngoại lệ json ánh xạ trong dịch vụ an toàn của tôi trong trường hợp json đầu vào không hợp lệ.Jersey/Jackson: làm thế nào để bắt ngoại lệ lập bản đồ json?
Nó ném org.codehaus.jackson.map.JsonMappingException
, nhưng tôi không biết cách hoặc nơi bắt ngoại lệ này. Tôi muốn bắt ngoại lệ này và gửi lại phản hồi lỗi thích hợp.
@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();
}
}
Mọi thứ hoạt động tốt, nhưng nếu thân hình json không được tạo hình tốt thì nó sẽ ném ra JsonMappingException
ngoại lệ. Tôi muốn bắt ngoại lệ này.
Xem thêm: https://java.net/jira/browse/JERSEY-2722 –