基于jersey的resuftul开发教程

1、环境:

    jersey版本:2.23.2

   下载地址为:https://jersey.java.net/,下载文件为:jaxrs-ri-2.23.2.zip

2、搭建开发环境:

      1)创建dynamic web project

      2)创建src,如下结构

        customer包:作为实体和对外服务的包;一个工程可以有多个这样的包,代表了对外提供的不同的服务。

       application包:注册相关服务的包;一个工程可以有一个这样的包就可以了,用来注册相关的服务包

          

    3)加入相关的jar包

       将jaxrs-ri-2.23.2.zip下的api、ext、lib中的所有jar都copy到项目的lib中,并增加jackson-all-1.9.0.jar,结果如下:


3、代码编写

1)编写实体类:Customer。@XmlRootElement是必须的哦!

    实体类就是客户端要提交到服务端的数据或者是服务端要返回给客户端的数据。实体类不是必须的,如果所交换的数据比较简单,也可以完全不用建立实体类。建立实体类的目的只是为了方便处理一些比较复杂的数据,比如实体类与xml,json之间的自由转换。

package rest.customer;


import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement
public class Customer {
private String firstName;
private String lastName;
private String street;
private String city;
private String state;
private String zip;
private String country;
private int id;


public Customer(){}
public Customer(String firstName, String lastName, String street, String city, String state, String zip,
String country, int id) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.street = street;
this.city = city;
this.state = state;
this.zip = zip;
this.country = country;
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}


public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
  2)编写服务类:服务类就是对外的接口类。客户端调用服务时,这个类是入口。

package rest.customer;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


@Path("/customer")
public class CustomerResourceService {  
    
    @GET  
    @Produces(MediaType.TEXT_PLAIN)  
    public String sayHello() {  
        return "Hello World!" ;  
    } 
  
  @GET  
  @Path("/{param}")  
  @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})  
public Customer getCustomer(@PathParam("param") int id) {
final Customer customer = new Customer("zhangzhi","yongdada","国贸","国贸","北京","100000","中国",1);  
return customer;
}
}

3)编写应用管理类:通过这个类,可以注册多个对外的restful服务以及相关的数据转换,日志等类。

package rest.application;

import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.glassfish.jersey.server.ResourceConfig;

import rest.customer.CustomerResourceService;


public class CustomerApplication extends ResourceConfig{


public CustomerApplication(){
//注册资源类
register(CustomerResourceService.class);

//注册数据转换类
register(JacksonJsonProvider.class);
}

}

4、修改配置文件 web.xml,增加jersey的支持

 
        restfulServelt
        org.glassfish.jersey.servlet.ServletContainer
       
            javax.ws.rs.Application
            rest.application.CustomerApplication        
       

        
        1
   

   
        restfulServelt
        /rest/*
   

5、浏览器测试

   

6、java aplication测试:

    1) 新建一个java project

     2)加入相关的jar包,这些包都来源于jaxrs-ri-2.23.2.zip中,详细如下:

   

  3)编写测试类:

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

public class MyTest 
{


    private static String serverUri = "http://localhost:7777/restapi/rest/customer"; 

    public static void main(String[] args){
    Client client =ClientBuilder.newClient();
    WebTarget target = client.target(serverUri);  
   
        Response response = target.path("/1").request(MediaType.APPLICATION_XML).get();
         
        String user = response.readEntity(String.class);  
        System.out.println(user);
        response.close();           
    }   
}

输出结果如下:

国贸中国zhangzhi1yongdada北京国贸100000


如果将请求的MIME类型改为json,即

Response response = target.path("/1").request(MediaType.APPLICATION_JSON).get();

输入结果如下:

{"firstName":"zhangzhi","lastName":"yongdada","street":"国贸","city":"国贸","state":"北京","zip":"100000","country":"中国","id":1}


   

你可能感兴趣的:(WEB,DEPLOYMENT)