jersey学习之二(基于maven的例子)基于1.x的jersey

来源:互联网 发布:淘宝c店还能做吗 编辑:程序博客网 时间:2024/05/16 15:06
  注意jersey1.x 与jersey2.x的区别是挺大的,包从sun换成了glassfish
1、官方文档
     https://jersey.java.net/documentation/latest/getting-started.html
     注意官网第二章阐述:
Until version 2.6, Jersey was compiled with Java SE 6. This has changes in Jersey 2.7. Now almost all Jersey components are compiled with Java SE 7 target. It means, that you will need at least Java SE 7 to be able to compile and run your application that is using latest Jersey. Only core-common and core-client modules are still compiled with Java class version runnable with Java SE 6.

自从jersey 2.6之后,jersey组件就是开始使用jdk7编译了。。所以项目中使用时要注意版本问题。如果下载最新的jersey必须在jdk7上运行. 切记。
         根据官方文档第一章开头所述,可以根据如下方式快速创建一个基于jersey发布在 Grizzly 容器的例子。
a、配置pom

Note

In case you want to depend on the latest SNAPSHOT versions of Jersey modules, the following repository configuration needs to be added to your Maven project pom:

<repository>
    <id>snapshot-repository.java.net</id>
    <name>Java.net Snapshot Repository for Maven</name>
    <url>https://maven.java.net/content/repositories/snapshots/</url>
    <layout>default</layout>
</repository>
b、运行maven命令
mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 
-DarchetypeGroupId=org.glassfish.jersey.archetypes 
-DinteractiveMode=false -DgroupId=com.example 
-DartifactId=simple-service -Dpackage=com.example 
-DarchetypeVersion=2.10.1
执行成功后将在maven/bin目录下得到一个simple-service的工程。。可以将工程导入eclipse 然后再转换成maven 项目

注意:
上述默认会使用jdk7,导入eclipse后直接修改成1.6还是会出现异常
运行上面的命令得到的工程在jdk 1.6环境中会报错:Unsupported major.minor version 51.0
即使修改了本地编译环境为1.6和pom文件中插件的编译环境为1.6编译成功。但也解除不了。。。需要修改jersey的2.10.1的版本为低版本的 。
实践过:2.0可以。
感觉高版本grizzlnio轻量级的内嵌http服务器是使用JDK7启动的。而项目中使用jdk6编译的,将项目中的jdk版本换成7可以简单地解决这个问题版本不兼容的问题。。(问题原因上面红色字体已经阐述了,JDK版本问题。2.6以后的版本需要在JDK7上运行)
最佳实践:
开发中要保证本地开发环境发布到服务器的运行环境版本一致

查看官网总结:
     根据官网user-guide的文档可以很容器创建 一个project和java EE Web Application,
都是基于MAVEN的项目。实践一下。

2、实现jersey的小应用(基于maven的项目,发布到tomcat中)(1.x版本,后面会附上2.x版本的配置)
  a、首先要引入所需jar包
可以配置如下pom文件,添加依赖
 <!-- Jersey -->
                <dependency>
                        <groupId> com.sun.jersey</groupId >
                        <artifactId> jersey-server</artifactId >
                        <version> 1.13</version >
                </dependency>
                <dependency>
                        <groupId> com.sun.jersey</groupId >
                        <artifactId> jersey-bundle</artifactId >
                        <version> 1.13</version >
                </dependency>
                <dependency>
                        <groupId> com.sun.jersey</groupId >
                        <artifactId> jersey-servlet </artifactId>
                        <version> 1.13</version >
                </dependency>
                <dependency>
                      <groupId> com.sun.jersey</groupId >
                      <artifactId> jersey-json </artifactId>
                      <version> 1.18.1</version >
                </dependency>
       <!--Jersey end  -->

2、web.xml中的配置
 <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.hpf.restfullServices</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>

指定拦截的路径和service所在的包路径
3、一个简单的服务端例子:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path ("/hello" )
public class Hello {
/**
 * Method handling HTTP GET requests. The returned object will be sent
 * to the client as "text/plain" media type.
 *
 * @return String that will be returned as a text/plain response.
 */      

       @GET
       @Produces(MediaType.TEXT_PLAIN)
       public String sayHello(){
             return "hello jersey" ;
      }
}



4、发布到tomcat中
客户端通过http://127.0.0.1:8080/jersey-web/rest/hello访问得到返回的值


使用jersey2.x的配置:
pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>com.hpf.jersey</groupId>  <artifactId>jersey-web</artifactId>  <packaging>war</packaging>  <version>0.0.1-SNAPSHOT</version>  <name>jersey-web Maven Webapp</name>  <url>http://maven.apache.org</url>  <distributionManagement>  <repository>  <id>snapshot-repository.java.net</id>    <name>Java.net Snapshot Repository for Maven</name>    <url>https://maven.java.net/content/repositories/snapshots/</url>    <layout>default</layout>  </repository>  </distributionManagement>  <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>3.8.1</version>      <scope>test</scope>    </dependency>    <!-- Jersey -->               <!--  <dependency>                        <groupId>com.sun.jersey</groupId>                        <artifactId>jersey-server</artifactId>                        <version>1.13</version>                </dependency>                <dependency>                        <groupId>com.sun.jersey</groupId>                        <artifactId>jersey-bundle</artifactId>                        <version>1.13</version>                </dependency>                <dependency>                        <groupId>com.sun.jersey</groupId>                        <artifactId>jersey-servlet</artifactId>                        <version>1.13</version>                </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-json</artifactId> <version>1.18.1</version></dependency> --><!--Jersey end  --><dependency>    <groupId>org.glassfish.jersey.containers</groupId>    <!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core"  -->    <artifactId>jersey-container-servlet</artifactId>    <version>2.5</version></dependency><!-- Required only when you are using JAX-RS Client --><dependency>    <groupId>org.glassfish.jersey.core</groupId>    <artifactId>jersey-client</artifactId>    <version>2.5</version></dependency>                <dependency>                        <groupId>javax.servlet</groupId>                        <artifactId>servlet-api</artifactId>                        <version>2.5</version>                </dependency>                  </dependencies>  <build>    <finalName>jersey-web</finalName>  </build></project>


web.xml
<?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>jersey</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><!-- jersey1.x的实现类 --><!--   <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.hpf.restfullServices</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> -->   <servlet>        <servlet-name>Jersey REST Service</servlet-name>        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>        <init-param>            <param-name>jersey.config.server.provider.packages</param-name>            <param-value>com.hpf.restfullServices</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>

java
package com.hpf.restfullServices;import java.net.URLEncoder;import javax.ws.rs.Consumes;import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.ws.rs.PathParam;import javax.ws.rs.Produces;import javax.ws.rs.core.MediaType;import org.glassfish.jersey.CommonProperties;@Path("/hello")public class Hello {/** * Method handling HTTP GET requests. The returned object will be sent * to the client as "text/plain" media type. *  * @return String that will be returned as a text/plain response. */@Path("/")@GET@Consumes(MediaType.TEXT_PLAIN+";charset=UTF-8")@Produces(MediaType.TEXT_PLAIN+";charset=UTF-8")public String sayHello(String username) throws Exception{return "w";}}










1 0
原创粉丝点击