Servlet 2.x Container
1、首先添加依赖
<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>
2、
web.xml基础配置两种实现方式
[align=left]利用servlet[/align]
<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 Filter123456789101112131415 | < 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 > |
The content of the
<init-param>
element will vary depending on the way you decide to configure Jerseyresources.以下是两种<init-param>的配置方式,可以根据需要选择一种。
第一种4、提供者和资源包的扫描
Example 4.12. Configuring Jersey container Servlet or Filter to use package scanning12345678910 | < 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 isenabled.总结:通过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 Application subclassIf you extend the Application class to provide the list of relevant root resourceclasses (getResources() ) and singletons (getSingletons() ),i.e. your JAX-RS application model, you then need to register it in your web application web.xml deployment descriptorusing a Servlet or Servlet filter initialization parameter with a name ofjavax.ws.rs.Application [sic] as follows:Example 4.11. Configuring Jersey container Servlet or Filter to use custom Application subclass1234 | < 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() methodsof your Application implementation.NoteThe name of the configuration property as defined by JAX-RS specification is indeed javax.ws.rs.Application and not javax.ws.rs.core.Application asone 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
1、首先添加依赖<!--jersey 2.x用以下依赖,但2.6以后的版本都是用是JDK7,所以要注意版本问题-->[align=left] <dependency >[/align][align=left]<groupId >org.glassfish.jersey.containers </groupId>[/align]<!-- if your container implementsServlet API older than 3.0, use "jersey-container-servlet-core" -->[align=left]<artifactId >jersey-container-servlet </artifactId>[/align][align=left]<version >2.5 </version>[/align][align=left]</dependency>[/align][align=left]<!-- Required only when you are using JAX-RS Client -->[/align][align=left]<dependency>[/align][align=left]<groupId >org.glassfish.jersey.core </groupId>[/align][align=left]<artifactId >jersey-client </artifactId>[/align][align=left]<version >2.5 </version>[/align][align=left]</dependency>[/align]2、
web.xml基础配置两种实现方式
[align=left]利用servlet[/align]
< 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 Filter123456789101112131415 | < 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 > |
The content of the
<init-param>
element will vary depending on the way you decide to configure Jerseyresources.以下是两种<init-param>的配置方式,可以根据需要选择一种。
第一种4、提供者和资源包的扫描
Example 4.12. Configuring Jersey container Servlet or Filter to use package scanning12345678910 | < 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 isenabled.总结:通过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 Application subclassIf you extend the Application class to provide the list of relevant root resourceclasses (getResources() ) and singletons (getSingletons() ),i.e.your JAX-RS application model, you then need to register it in your web application web.xml deployment descriptor using a Servlet or Servletfilterinitialization parameter with a name ofjavax.ws.rs.Application [sic] as follows:Example 4.11. Configuring Jersey container Servlet or Filter to use custom Application subclass1234 | < 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() methodsof your Application implementation.NoteThe 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 mightexpect.importorg.glassfish.jersey.server.ResourceConfig; importorg.glassfish.jersey.server.spring.scope.RequestContextFilter; publicclassMyapplicationextendsResourceConfig{ publicMyapplication(){ register(RequestContextFilter.class); register(Hello.class); } } 在Myapplication中暴漏资源 |