jersey实现rest实例之二

来源:互联网 发布:麻瓜网络 编辑:程序博客网 时间:2024/06/06 19:04

之前有写过jersey1.18实现rest方式,这里介绍jersey2.x实现rest获取text,xml,json格式数据

 

测试项目开发部署环境

开发工具:eclipse-jee-juno-SR2-win32

JDK:jdk-7u3-windows-i586

应用服务器:glassfish-4.0

REST实现框架:jersey2.5.1

项目名称:TestJersey2.5

这里应用服务器选择glassfish-4.0,有两个原因

1.jersey2.x版本实现的是JAX-RS 2.0规范,glassfish-4.0版本之前的支持的是JAX-RS 1.0规范,glassfish-4.0要求运行版本不低于jdk7。

2.jersey2.x版本部署到tomcat里面,在返回json格式数据时,会抛异常,jersey2.x在返回json时需要glassfish提供类型支持。

Jersey2.5集成步骤:

1.新建一个动态web工程:TestJersey2.5

2.引入jar包,解压jersey2.5压缩包,将lib、ext、api中的jar文件全部引入项目

3.配置web.xml,这里主要配置jersey自己servlet,配置清单:

<?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"

xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

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>TestJersey2.5</display-name>
  
  <servlet>
        <servlet-name>jersey_rest</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.toplucky.TestApp</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>jersey_rest</servlet-name>
        <url-pattern>/jars/*</url-pattern>
    </servlet-mapping>
</web-app>

 

4.通过实现javax.ws.rs.Application来存放所有的rest服务资源,代码清单:

package com.toplucky;

import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.server.ResourceConfig;

@ApplicationPath("/jars")
public class TestApp extends ResourceConfig{

    public TestApp() {
        packages("com.toplucky.resource");
        register(LoggingFilter.class);
    }
}

 5.rest服务资源定义在指定包下面,这里定义一个测试服务TestResource放在com.toplucky.resource下面,代码清单:

 package com.toplucky.resource;
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.MediaType;
 import com.toplucky.TestBean;


@Path("test")
public class TestResource {
@Path("getText")
    @GET
    @Produces("text/plain")
    public String getText() {
        return "hello lucky";
    }

@Path("getXml")
@GET
@Produces(value=MediaType.APPLICATION_XML)
    public TestBean getXml(){
    return new TestBean("a", 26, 62);
    }

@Path("getJson")
@GET
@Produces(value=MediaType.APPLICATION_JSON)
    public TestBean getJson(){
    return new TestBean("a", 26, 62);
    }
}

6.相关bean,代码清单:

package com.toplucky;

import javax.xml.bind.annotation.XmlRootElement;

 

@XmlRootElement

public class TestBean {

    public String a;

    public int b;

    public long c;

    public TestBean() {

    }

    public TestBean(String a, int b, long c) {

        this.a = a;

        this.b = b;

        this.c = c;

    }

    public String getA() {

        return a;

    }

    public int getB() {

        return b;

    }

    public long getC() {

        return c;

    }

    @Override

    public boolean equals(Object o) {

        if (this == o) return true;

        if (o == null || getClass() != o.getClass()) return false;

        TestBean that = (TestBean) o;

        if (b != that.b) return false;

        if (c != that.c) return false;

        if (a != null ? !a.equals(that.a) : that.a != null) return false;

        return true;

    }

    @Override

    public int hashCode() {

        int result = a != null ? a.hashCode() : 0;

        result = 31 * result + b;

        result = 31 * result + (int) (c ^ (c >>> 32));

        return result;

    }

}

  到这里测试jersey2.5的准备工作都已经完成,剩下的就是将TestJersey2.5项目部署到GlassFish4.0中并发布,

  这里可能需要下载GlassFish for eclipse插件,方便在eclipse工具中快速部署与发布,我这里截一个集成后的图。

         

请求文本数据:http://localhost:8080/TestJersey2.5/jars/test/getText

界面显示结果:hello lucky


请求xml数据:http://localhost:8080/TestJersey2.5/jars/test/getXml

界面显示结果:

<testBean>
<a>a</a>
<b>26</b>
<c>62</c>
</testBean>

请求json数据:http://localhost:8080/TestJersey2.5/jars/test/getJson
界面显示结果:{"a":"a","b":26,"c":62}

说明:如果请求的只是文本、xml数据,那么应用服务器可以选择tomcat,jdk版本也可以用jdk6。

 测试项目下载地址 : http://download.csdn.net/detail/check_null/6836009







0 0
原创粉丝点击