码迷,mamicode.com
首页 > Web开发 > 详细

jersey构建rest服务返回json数据

时间:2016-08-01 22:33:20      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

1.  eclipse 创建 dynamic web project

2.  将jersey相关jar包放到libs目录下

技术分享

3. web.xml 增加 jersey 相关内容

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <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>Jersey REST Service</servlet-name>
	<servlet-class>
	  com.sun.jersey.spi.container.servlet.ServletContainer
	</servlet-class>
	  <init-param>
	    <param-name>com.sun.jersey.config.property.packages</param-name>
	    <param-value>com.huawei.rest.resources</param-value>
	  </init-param>
	  <load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
	  <servlet-name>Jersey REST Service</servlet-name>
	  <url-pattern>/rest/*</url-pattern>
	</servlet-mapping>
  
</web-app>

 4. 创建 resource 类

package com.huawei.rest.resources;

import javax.ws.rs.GET;  
import javax.ws.rs.Path;  
import javax.ws.rs.Produces;  
import javax.ws.rs.PathParam;  
import javax.ws.rs.core.MediaType;  
  
@Path("/hello")  
public class HelloResource {  
    @GET  
    @Produces(MediaType.TEXT_PLAIN)  
    public String sayHello() {  
        return "hello jersey , first demo" ;  
    }  
   
      
    @GET  
    @Path("/{param}")    
    @Produces("text/plain;charset=UTF-8")  
    public String sayHelloToUTF8(@PathParam("param") String username) {  
        return "Hello " + username;  
    }  
    
    @GET  
    @Path("/getuser")  
    @Produces(MediaType.APPLICATION_JSON)  
    public User getUserJson() {  
     User user  = new User();  
     user.setAge(27);  
     user.setUserid("005");  
     user.setUsername("Fmand");  
     return user;  
    }
      
}  

5. 创建 user 类

package com.huawei.rest.resources;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class User {
	int age;
	String userid;
	String username;
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		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;
	}
}

6. 运行一下看看效果

技术分享

 

源代码下载地址: http://download.csdn.net/detail/mtour/9592162 

 

jersey构建rest服务返回json数据

标签:

原文地址:http://www.cnblogs.com/mtour/p/5727380.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!