rest - Spring MVC Restful service response -
any thought why "http/1.1 200 ok" when set response.status(response.status.not_found). can see has been set correctly in response body?
curl -v http://my_host/api/v1/user/99999999
http/1.1 200 ok
access-control-allow-origin: *
access-control-allow-methods: post, get, options, delete
....
{"statustype":"not_found","entity":"unable reterive product id:99999999","entitytype":"java.lang.string","status":404,"metadata":{}}
@requestmapping(value="/product/{id}", method=requestmethod.get) @responsebody public response getproduct(@pathvariable string id) { product product = null; //productservice.getproduct(id); if (product == null) { // know here !!! homecoming response.status(response.status.not_found).entity("unable retrieve product id:"+id). build(); } // expected not here map<string, object> json = productrenderer.renderproduct(....); homecoming response.ok(json, mediatype.application_json).type("application/json").build(); }
btw using spring version 3.2.10
try returning spring's responseentity
instead. works me , sets right response status:
for example:
return new responseentity<>(httpstatus.not_found);
or body:
return new responseentity<>(body, httpstatus.ok);
you can utilize builder pattern response
in question (following illustration responseentity's javadoc:
return responseentity .created(location) .header("myresponseheader", "myvalue") .body("hello world");
more details can found in documentation:
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/responseentity.html
spring rest spring-mvc java-ee
No comments:
Post a Comment