Amacım, bir nesne bulunmadığında bir açıklayıcı mesajla birlikte 404'te döndürülen bir hata çekirdeğinin bulunması ve istenen MIME türünün döndürülmesidir.Jersey'deki İstisnalar İçin JSON veya XML Dönüyor
ben başlığı Kabul ihtiyacım yok URI dayalı XML veya JSON belirtilen nesne (Ben kurulum böylece com.sun.jersey.config.property.resourceConfigClass servlet parametresini dönecektir bir bakmak kaynak var JAXBContextResolver, tür listesinde ErrorBean.class var ve bu sınıf için doğru JAXBContext döndürülür çünkü günlükleri görebiliyorum).
örn: http://foobar.com/rest/locations/1.json
@GET
@Path("{id}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Location getCustomer(@PathParam("id") int cId) {
//look up location from datastore
....
if (location == null) {
throw new NotFoundException("Location" + cId + " is not found");
}
}
Ve NotFoundException şöyle görünür:
public class NotFoundException extends WebApplicationException {
public NotFoundException(String message) {
super(Response.status(Response.Status.NOT_FOUND).
entity(new
ErrorBean(
message,
Response.Status.NOT_FOUND.getStatusCode()
)
.build());
}
}
ErrorBean aşağıdaki gibidir: Ancak
@XmlRootElement(name = "error")
public class ErrorBean {
private String errorMsg;
private int errorCode;
//no-arg constructor, property constructor, getter and setters
...
}
, hep bir alıyorum 204 Denediğimde İçerik Yok yanıtı bu. Etrafa hacklenmiş ettim ve bir dize döndürür ve mim türünü belirtirseniz bu iyi çalışır:
public NotFoundException(String message) {
super(Response.status(Response.Status.NOT_FOUND).
entity(message).type("text/plain").build());
}
Ben de bir kaynak olarak bir ErrorBean dönen çalıştılar. Bu çalışıyor:
gelecekte benzer sorunları olanlar için{"errorCode":404,"errorMsg":"Location 1 is not found!"}