问题描述
我使用带有Jersey 2.7的ResourceConfig从web.xml迁移到完全Java配置并部署在Tomcat 7上.之后,我无法再使用与web.xml方法相同的URL来访问服务. .我不明白ResourceConfig如何影响路径.
I migrated from web.xml to totally Java configuration using ResourceConfig with Jersey 2.7 and deploying on Tomcat 7. After that I am not able to reach the services anymore by using the same urls that I was using with the web.xml approach. I don't understand how the ResourceConfig is affecting the paths.
我以前的web.xml
My previous 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_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>my.app</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.mypackage.resource,com.mypackage.providers</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.scanning.recursive</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.filter.LoggingFilter</param-value>
</init-param>
<init-param>
<param-name>org.glassfish.jersey.server.ServerProperties.BV_SEND_ERROR_IN_RESPONSE</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>my.app</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
我扩展ResourceConfig的配置类是:
My configuration class that extends ResourceConfig is:
MyRESTAPIApp.java
MyRESTAPIApp.java
@ApplicationPath("")
public class MyRESTAPIApp extends ResourceConfig{
public MyRESTAPIApp () {
packages("com.mypackage.resource", "com.mypackage.providers");
register(org.glassfish.jersey.filter.LoggingFilter.class);
property("jersey.config.beanValidation.enableOutputValidationErrorEntity.server", "true");
}
}
我的资源之一是:
FlagResource.java
FlagResource.java
@Path("my-resource")
public class FlagResource {
private MyService myService = new MyService();
@GET
@Produces(MediaType.APPLICATION_JSON)
public FlagResource getFlagResource(@NotNull @QueryParam("level") Long level) {
FlagResource flagResource = myService.getFlagResource(level);
return flagResource;
}
}
我正在引发的战争叫做:my.app.war.
The war that I am generating is called: my.app.war.
Tomcat照常使用war文件名中的Web上下文根路径,但是我不知道在使用基于Java代码的配置时,它是否会更改.
Tomcat was taking the web context root path from the name of the war file as usual, but I don't know if that changes when using Java code based configuration.
GET http://localhost:8080/my.app/my-resource?level=1
返回404
推荐答案
实际上,我通过添加"/"作为@ApplicationPath批注的值来解决了这个问题,我认为这是没有必要的,因为API文档对@进行了以下说明ApplicationPath值参数:
Actually I solved this by adding "/" as the value of the @ApplicationPath annotation, I thought it was not necessary because the API documentation says the following for @ApplicationPath value param:
Defines the base URI for all resource URIs. A trailing '/' character will be automatically appended if one is not present.
我假设保留一个空String等效于使用@ApplicationPath("/"),但不是.
I assumed that leaving an empty String will be equivalent to use @ApplicationPath("/") but it is not.
这是配置类现在的外观:
So this is how the configuration class looks now:
@ApplicationPath("/")
public class MyRESTAPIApp extends ResourceConfig{
public MyRESTAPIApp () {
packages("com.mypackage.resource", "com.mypackage.providers");
register(org.glassfish.jersey.filter.LoggingFilter.class);
property("jersey.config.beanValidation.enableOutputValidationErrorEntity.server", "true");
}
}
这篇关于不带web.xml的使用ResourceConfig的Jersey应用程序的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!