jersey學習之二(基於maven的例子)基於1.x的jersey

  注意jersey1.x 與jersey2.x的區別是挺大的,包從sun換成了glassfish
1、官方文檔
     注意官網第二章闡述:
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>
    <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";
		
	}
}










發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章