i'm using jersey provide java rest service outside world. offer functions take json , use jackson framework in combination jersey convert them pojos.
i have problem if wrong jackson format sent server, answer (content of http response) jackson specific exception description. if have pojo attribute "surname" , send "sursname" in json string server, get:
unrecognized field "sursname" (class xy), not marked ignorable @ [source: sun.net.httpserver.fixedlengthinputstream@903025; line: 1, column: 49] (through reference chain: xy["sursname"])
thats quite ok, have own response content, example own error code. wrote custom exceptionmapper should map throwables.
@provider public class webserviceexceptionmapper implements exceptionmapper<throwable> { @override public response toresponse(exception e) { e.printstacktrace(); return response.status(400).entity("{\"errorcode\":\"critical_error\"}").type(mediatype.application_json).build(); } }
it seems jackson exception thrown before webservice method called, have no chance map it?
does have idea? big und sorry english ;)
the reason custom exceptionmapper doesn't catch exceptions because jackson library provides own exception mappers, catches exception before catched genereal exception mapper.
some suggest should implement own exception mappers jsonparseexceptionmapper , jacksonmappingexceptionmapper, that, however, give inconsistent results. see issue on github.
to solve problem, must make sure none of built-in exception mappers registered. if you're using jackson-jaxrs-providers library json: jackson-jaxrs-json-provider, make sure only registering either jacksonjaxbjsonprovider.class or jacksonjsonprovider.class in application class:
public class myapplication extends resourceconfig { public myapplication() { register(jacksonjaxbjsonprovider.class) } }
be aware of jacksonfeature.class, registers built in exceptionmappers. stay away jersey-media-json-jackson library, automatically add built in exception mappers, without having @ all.
Comments
Post a Comment