程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> jersey構建rest服務返回json數據,jersey構建restjson

jersey構建rest服務返回json數據,jersey構建restjson

編輯:JAVA綜合教程

jersey構建rest服務返回json數據,jersey構建restjson


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 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved