1、XML数据类型输出:
一、 总体说明
XML和JSON 是最为常用的数据交换格式。本例子演示如何将java对象,转成XML输出。
二、流程
1.在上文的例子中,创建一个包“com.waylau.rest.bean”
2.在该包下创建一个JAVA类"User"
[java] view plain
copy
print ?
- package com.waylau.rest.bean;
- import javax.xml.bind.annotation.XmlRootElement;
-
-
-
-
-
- @XmlRootElement
- public class User {
-
- private String userId;
- private String userName;
- private String age;
-
- public User() {};
-
- public User(String userId, String userName, String age) {
- this.userId = userId;
- this.userName = userName;
- this.age = age;
- }
- public String getUserId() {
- return userId;
- }
- public void setUserId(String userId) {
- this.userId = userId;
- }
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String getAge() {
- return age;
- }
- public void setAge(String age) {
- this.age = age;
- }
- }
package com.waylau.rest.bean;
import javax.xml.bind.annotation.XmlRootElement;
/*
* 用户 bean
* @author waylau.com
* 2014-3-17
*/
@XmlRootElement
public class User {
private String userId;
private String userName;
private String age;
public User() {};
public User(String userId, String userName, String age) {
this.userId = userId;
this.userName = userName;
this.age = age;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
注意:该类上面增加了一个注解“@XmlRootElement”,在将该类转化成XML时,说明这个是XML的根节点。
3.在“com.waylau.rest.resources”中,增加资源“UserResource“,代码如下:
[java] view plain
copy
print ?
- package com.waylau.rest.resources;
-
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- import javax.ws.rs.Path;
- import javax.ws.rs.Produces;
- import javax.ws.rs.PathParam;
- import javax.ws.rs.core.MediaType;
- import javax.ws.rs.DELETE;
- import javax.ws.rs.GET;
- import javax.ws.rs.POST;
- import javax.ws.rs.PUT;
-
- import com.waylau.rest.bean.User;
-
- @Path("/users")
- public class UserResource {
- private static Map<String,User> userMap = new HashMap<String,User>();
-
-
-
-
- @GET
- @Produces(MediaType.APPLICATION_XML)
- public List<User> getAllUsers(){
- List<User> users = new ArrayList<User>();
- User u1 = new User("001","WayLau","26");
- User u2 = new User("002","King","23");
- User u3 = new User("003","Susan","21");
-
- userMap.put(u1.getUserId(), u1);
- userMap.put(u2.getUserId(), u2);
- userMap.put(u3.getUserId(), u3);
-
- users.addAll( userMap.values() );
- return users;
- }
-
- @GET
- @Path("/getUserXml")
- @Produces(MediaType.APPLICATION_XML)
- public User getUserXml() {
- User user = new User();
- user.setAge("21");
- user.setUserId("004");
- user.setUserName("Amand");
- return user;
- }
-
-
-
- }
其中MediaType.APPLICATION_XML 说明了是以XML形式输出
在浏览器输入http://localhost:8089/RestDemo/rest/users/getUserXml,输出单个对象
在浏览器输入 http://localhost:8089/RestDemo/rest/users 输出对象的集合
2、数据输出为JSON类型:
一、 总体说明
XML和JSON 是最为常用的数据交换格式。本例子演示如何将java对象,转成JSON输出。
二、流程
1.在上文项目中,
在“com.waylau.rest.resources.UserResource“中增加代码,代码如下:
[java] view plain
copy
print ?
- @GET
- @Path("/getUserJson")
- @Produces(MediaType.APPLICATION_JSON)
- public User getUserJson() {
- User user = new User();
- user.setAge("27");
- user.setUserId("005");
- user.setUserName("Fmand");
- return user;
- }
@GET
@Path("/getUserJson")
@Produces(MediaType.APPLICATION_JSON)
public User getUserJson() {
User user = new User();
user.setAge("27");
user.setUserId("005");
user.setUserName("Fmand");
return user;
}
MediaType.APPLICATION_JSON 说明输出的是JSON格式
2,运行项目,浏览器输入http://localhost:8089/RestDemo/rest/users/getUserJson
期望获取到json的数据,此时,项目报错
[html] view plain
copy
print ?
- 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)
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 下载地址http://wiki.fasterxml.com/JacksonDownload
4. 项目中引入jackson-all-1.9.11.jar
5.在“com.waylau.rest”目录下创建RestApplication.java
[java] view plain
copy
print ?
- package com.waylau.rest;
-
- import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
- import org.glassfish.jersey.filter.LoggingFilter;
- import org.glassfish.jersey.server.ResourceConfig;
-
-
-
-
-
-
- public class RestApplication extends ResourceConfig {
- public RestApplication() {
-
-
- packages("com.waylau.rest.resources");
-
- register(JacksonJsonProvider.class);
-
- }
- }
package com.waylau.rest;
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.server.ResourceConfig;
/**
* 应用
* @author waylau.com
* 2014-3-18
*/
public class RestApplication extends ResourceConfig {
public RestApplication() {
//服务类所在的包路径
packages("com.waylau.rest.resources");
//注册JSON转换器
register(JacksonJsonProvider.class);
}
}
6.修改web.xml,初始化从RestApplicaton进入应用,如下:
[html] view plain
copy
print ?
- <servlet>
- <servlet-name>Way REST Service</servlet-name>
- <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
- <init-param>
- <param-name>javax.ws.rs.Application</param-name>
- <param-value>com.waylau.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>
<servlet>
<servlet-name>Way REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.waylau.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>
7.运行项目,再次访问http://localhost:8089/RestDemo/rest/users/getUserJson
即可输出JSON文本