java - Return value of RESTful Web Service, taking a null value into account -


i creating client restful web service. in case, server not return expected object (e.g. nothing found on given parameter, return null).

@get @path("/person/{name}") @produces("application/xml") public person getperson(@pathparam("name") string name) {     return people.byname(name); } 

the byname() method return null if given name not found. upon unmarshalling given object, raise exception. common , clean way result in if/else statement or handle returns in different ways?

jaxbcontext jcunmarshaller = jaxbcontext.newinstance(person.class); unmarshaller unmarshaller = jcunmarshaller.createunmarshaller(); return (person) unmarshaller.unmarshal(connection.getinputstream()); 

solution

getperson( ) throws webapplicationexception {      throw new webapplicationexception(404); } 

the idiomatic way handle such situations return 404 not found response code. can throw webapplicationexception:

@get @path("/person/{name}") @produces("application/xml") public person getperson(@pathparam("name") string name) {     person personornull = people.byname(name);     if(personornull == null) {       throw new webapplicationexception(404);     }        return personornull; }  

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -