jesey
来源:互联网 发布:网络语雪梨是什么意思 编辑:程序博客网 时间:2024/06/07 00:34
jersye Guide: http://jersey.java.net/nonav/documentation/latest/user-guide.html
a point in the whole "REST way of thinking".
Short answer: yes, you can only call getEntity once. You need to check the returned HTTP status to know what entity you should get.
On the server side:
- When designing a REST API, one should always use appropriate status codes regarding the HTTP RFC.
- For that matter, please consider using the ExceptionMapper interface (here's an example with a "NotFoundException"
So, now your server returns either "HTTP status OK - 200" with a User object, or an error status with an error object.
On the client side:
You need to check the return status and adapt your behavior according to the API spec.here's a quick and dirty code sample:
ClientResponse response=null;response = webRsrc.accept(MediaType.APPLICATION_XML).post(ClientResponse.class,usr);int status = response.getStatus();if (Response.Status.OK.getStatusCode() == status) { // normal case, you receive your User object User usr = response.getEntity(User.class);} else { ErrorResponse err = response.getEntity(ErrorResponse.class);}
NB: depending on the status code returned, this error could be very different (thus needing very different behavior):
- client error 40X: your client request is wrong
- server error 500: an unexpected error occured on the server side
This kind of code could be used to manage Error message or business message in the response :
protected <T> T call(String uri, Class<T> c) throws BusinessException { WebResource res = new Client().create().resource(url); ClientResponse cresp = res.get(ClientResponse.class); InputStream respIS = cresp.getEntityInputStream(); try { // Managing business or error response JAXBContext jCtx = JAXBContext.newInstance(c, BeanError.class); Object entity = jCtx.createUnmarshaller().unmarshal(respIS); // If the response is an error, throw an exception if(entity instanceof BeanError) { BeanError error = (BeanError) entity; throw new BusinessException(error); // If this not an error, this is the business response } else { return (T) entity; } } catch (JAXBException e) { throw(new BusinessException(e)); }}
- jesey
- rest推送实现--jesey SSE
- MySQL audit 插件总结
- getHibernateTemplate().save不能插入数据
- Makefile资料
- Java方法的可变参数类型
- Activity的onSaveInstanceState()和 onRestoreInstanceState()方法
- jesey
- 枚举类
- Android 发送(彩信)MMS
- .net Delegate (二):揭秘
- Android WIFI网络操作
- java 修饰符的作用一(public protected default private 组)
- TCP/IP完整的基础介绍
- msdn zhongwen
- linux用户管理