Servlet 2.x Container
<pre name="code" class="html"><!--jersey 2.x用以下依賴,但2.6以後的版本都是用是JDK7,所以要注意版本問題-->
<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>
<pre name="code" class="html"><!--jersey 2.x用以下依賴,但2.6以後的版本都是用是JDK7,所以要注意版本問題-->
<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>
web.xml
基礎配置兩種實現方式
<web-app>
<servlet>
<servlet-name>MyApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
...
</init-param>
</servlet>
...
<servlet-mapping>
<servlet-name>MyApplication</servlet-name>
<url-pattern>/myApp/*</url-pattern>
</servlet-mapping>
...
</web-app>
利用過濾器:
Alternatively, you can register Jersey container as a filter:
Example 4.10. Hooking up Jersey as a Servlet Filter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
< web-app > < filter > < filter-name >MyApplication</ filter-name > < filter-class >org.glassfish.jersey.servlet.ServletContainer</ filter-class > < init-param > ... </ init-param > </ filter > ... < filter-mapping > < filter-name >MyApplication</ filter-name > < url-pattern >/myApp/*</ url-pattern > </ filter-mapping > ... </ web-app > |
<init-param>
element will vary depending on the way you decide to configure Jersey
resources.Example 4.12. Configuring Jersey container Servlet or Filter to use package scanning
1
2
3
4
5
6
7
8
9
10
|
< init-param > < param-name >jersey.config.server.provider.packages</ param-name > < param-value > org.foo.myresources,org.bar.otherresources </ param-value > </ init-param > < init-param > < param-name >jersey.config.server.provider.scanning.recursive</ param-name > < param-value >false</ param-value > </ init-param >
紅色部分是設置是否遞歸掃描子包,默認是true.設置爲false爲不遞歸掃描。
Jersey will automatically discover the resources and providers in the selected packages.
You can also decide whether Jersey should recursively scan also sub-packages by setting the
jersey.config.server.provider.scanning.recursive property.
The default value is
true , i.e. the recursive scanning of sub-packages is
enabled.
總結:通過1、2、4的步驟就可以在應用中使用jersey(環境要求:JDK 1.6/TOMCAT)。。因爲jersey 2.6版本的以上都是用JDK7。目前最新版本 2.10.1
而jersey 1.x版本實現類與2.x不同。前面有例子介紹。
第二種
3、擴展配置(不需要可以不配置)4.7.1.1. Custom
|
1
2
3
4
|
< init-param > < param-name >javax.ws.rs.Application</ param-name > < param-value >org.foo.MyApplication</ param-value > </ init-param > |
Jersey will consider all the classes returned by getResources()
and getSingletons()
methods
of your Application
implementation.
Note
javax.ws.rs.Application
and not javax.ws.rs.core.Application
as
one might expect.
importorg.glassfish.jersey.server.ResourceConfig;
importorg.glassfish.jersey.server.spring.scope.RequestContextFilter;
publicclassMyapplicationextendsResourceConfig{
publicMyapplication(){
register(RequestContextFilter.class);
register(Hello.class);
}
}
在Myapplication中暴漏資源
Servlet 2.x Container
<!--jersey 2.x用以下依賴,但2.6以後的版本都是用是JDK7,所以要注意版本問題-->
web.xml
基礎配置兩種實現方式
< web-app > < servlet > < servlet-name >MyApplication</ servlet-name > < servlet-class >org.glassfish.jersey.servlet.ServletContainer</ servlet-class > < init-param > ... </ init-param > </ servlet > ... < servlet-mapping > < servlet-name >MyApplication</ servlet-name > < url-pattern >/myApp/*</ url-pattern > </ servlet-mapping > ... </ web-app > |
利用過濾器:
Alternatively, you can register Jersey container as a filter:
Example 4.10. Hooking up Jersey as a Servlet Filter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
< web-app > < filter > < filter-name >MyApplication</ filter-name > < filter-class >org.glassfish.jersey.servlet.ServletContainer</ filter-class > < init-param > ... </ init-param > </ filter > ... < filter-mapping > < filter-name >MyApplication</ filter-name > < url-pattern >/myApp/*</ url-pattern > </ filter-mapping > ... </ web-app > |
<init-param>
element will vary depending on the way you decide to configure Jersey
resources.Example 4.12. Configuring Jersey container Servlet or Filter to use package scanning
1
2
3
4
5
6
7
8
9
10
|
< init-param > < param-name >jersey.config.server.provider.packages</ param-name > < param-value > org.foo.myresources,org.bar.otherresources </ param-value > </ init-param > < init-param > < param-name >jersey.config.server.provider.scanning.recursive</ param-name > < param-value >false</ param-value > </ init-param >
紅色部分是設置是否遞歸掃描子包,默認是true.設置爲false爲不遞歸掃描。
Jersey will automatically discover the resources and providers in the selected packages.
You can also decide whether Jersey should recursively scan also sub-packages by setting the
jersey.config.server.provider.scanning.recursive property.The default value is
true , i.e. the recursive scanning of sub-packages is
enabled.
總結:通過1、2、4的步驟就可以在應用中使用jersey(環境要求:JDK 1.6/TOMCAT)。。因爲jersey 2.6版本的以上都是用JDK7。目前最新版本 2.10.1
而jersey 1.x版本實現類與2.x不同。前面有例子介紹。
第二種
3、擴展配置(不需要可以不配置)4.7.1.1. Custom
|
1
2
3
4
|
< init-param > < param-name >javax.ws.rs.Application</ param-name > < param-value >org.foo.MyApplication</ param-value > </ init-param > |
Jersey will consider all the classes returned by getResources()
and getSingletons()
methods
of your Application
implementation.
Note
The name of the configuration property as defined by JAX-RS specification is indeed javax.ws.rs.Application
and not javax.ws.rs.core.Application
as one might
expect.
importorg.glassfish.jersey.server.ResourceConfig;
importorg.glassfish.jersey.server.spring.scope.RequestContextFilter;
publicclassMyapplicationextendsResourceConfig{
publicMyapplication(){
register(RequestContextFilter.class);
register(Hello.class);
}
}
在Myapplication中暴漏資源