标签:
一、 总体说明
XML和JSON 是最为常用的数据交换格式。本例子演示如何将JAVA对象,转换成JSON输出。
二、流程
1.在上文项目中,
在“cn.itrush.rest.resources.UserResource“中增加代码,代码如下:
1 package cn.itrush.rest.resources; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 import javax.ws.rs.Path; 9 import javax.ws.rs.Produces; 10 import javax.ws.rs.PathParam; 11 import javax.ws.rs.core.MediaType; 12 import javax.ws.rs.DELETE; 13 import javax.ws.rs.GET; 14 import javax.ws.rs.POST; 15 import javax.ws.rs.PUT; 16 17 import cn.itrush.rest.bean.User; 18 19 /** 20 * 回嘉 21 * @author huiqinbo.cn 22 * 2016-8-24 23 */ 24 25 @Path("/users") 26 public class UserResource { 27 private static Map<String,User> userMap = new HashMap<String,User>();//?洢?û? 28 29 /** 30 * 返回多节点XML格式文件 31 * @return 32 */ 33 @GET 34 @Produces(MediaType.APPLICATION_XML) 35 public List<User> getAllUsers(){ 36 List<User> users = new ArrayList<User>(); 37 User u1 = new User("001","HuiJia","26"); 38 User u2 = new User("002","Andy","23"); 39 User u3 = new User("003","BoWen","21"); 40 41 userMap.put(u1.getUserId(), u1); 42 userMap.put(u2.getUserId(), u2); 43 userMap.put(u3.getUserId(), u3); 44 45 users.addAll( userMap.values() ); 46 return users; 47 } 48 49 50 /** 51 * 返回单节点XML格式文件 52 * @return 53 */ 54 @GET 55 @Path("/getUserXml") 56 @Produces(MediaType.APPLICATION_XML) 57 public User getUserXml() { 58 User user = new User(); 59 user.setAge("21"); 60 user.setUserId("004"); 61 user.setUserName("回嘉"); 62 return user; 63 } 64 65 /** 66 * 返回单节点Json格式文件 67 * @return 68 */ 69 @GET 70 @Path("/getUserJson") 71 @Produces(MediaType.APPLICATION_JSON) 72 public User getUserJson(){ 73 User user = new User(); 74 user.setAge("30"); 75 user.setUserId("2"); 76 user.setUserName("回嘉"); 77 return user; 78 } 79 }
注:红色部分为新增J的AVA对象返回JSON 的代码,其它部分是第2节的内容
注:MediaType.APPLICATION_JSON 说明输出的是JSON格式
2,运行项目,浏览器输入http://localhost:8080/RestDemo/rest/users/getUserJson
期望获取到json的数据,此时,项目报错
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class com.waylau.rest.bean.User, genericType=class com.waylau.rest.bean.User. at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247) at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162) at org.glassfish.jersey.filter.LoggingFilter.aroundWriteTo(LoggingFilter.java:293) at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162) at org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:103) at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162) at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:88) at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162) at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1154) at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:571) at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:378) at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:368) at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:262)
浏览器报错页面显示:
此时,需要获取json转换包的支持 。
可以由多种方式实现:MOXy、JSON-P、Jackson、Jettison等,本例为Jackson。
3. jackson-all-1.9.11.jar 下载地址我是从CSND(http://download.csdn.net/detail/wind520/5674963)上面下载一个。
4. 项目中引入jackson-all-1.9.11.jar
注:将jackson-all-1.9.11.jar先放入项目工程的文件夹里,再刷新项目;如果直接放入项目内会报错
5.在“cn.itrush.rest”目录下创建RestApplication.java
1 package cn.itrush.rest; 2 3 import org.codehaus.jackson.jaxrs.JacksonJsonProvider; 4 import org.glassfish.jersey.filter.LoggingFilter; 5 import org.glassfish.jersey.server.ResourceConfig; 6 7 /** 8 * 回嘉 9 * @author huiqinbo.cn 10 * 2016-8-24 11 */ 12 public class RestApplication extends ResourceConfig { 13 public RestApplication() { 14 15 //服务类所在的包路径 16 packages("cn.itrush.rest.resources"); 17 //注册JSON转换器 18 register(JacksonJsonProvider.class); 19 20 } 21 }
6.修改web.xml,初始化从RestApplicaton进入应用,如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID"> <display-name>RestDemo</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>Way REST Service</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <!-- <param-name>jersey.config.server.provider.packages</param-name> <param-value>cn.itrush.rest.resources</param-value> --> <param-name>javax.ws.rs.Application</param-name> <param-value>cn.itrush.rest.RestApplication</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Way REST Service</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app>
注:注释部分是转换成XML时用的。下面是转换成JSON时用的。
7.运行项目,再次访问http://localhost:8080/RestDemo/rest/users/getUserJson
即可输出JSON文本
JSON格式里有乱码问题,以后再解决。
无废话Jersey构建RESTful服务之WebService系统教程 --3 [JAVA对象转成JSON输出]
标签:
原文地址:http://www.cnblogs.com/itrush/p/5802230.html