I would like to catch json mapping exception in my restful service in case input json is not valid.
It throws org.codehaus.jackson.map.JsonMappingException
, but I don't how to or where to catch this exception. I want to catch this exception and send back appropriate error response.
@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();
}
}
Everything works fine, but if json body is not well formed then it throws JsonMappingException
exception. I want to catch this exception.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…