ras*_*orp 5 java jersey tomcat7
我使用带有Jersey 2.7的ResourceConfig并在Tomcat 7上进行部署,从web.xml迁移到完全Java配置.之后,我无法使用与web.xml方法相同的URL来访问服务.我不明白ResourceConfig如何影响路径.
我以前的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>
Run Code Online (Sandbox Code Playgroud)
扩展ResourceConfig的配置类是:
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");
}
}
Run Code Online (Sandbox Code Playgroud)
我的一个资源是:
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;
}
Run Code Online (Sandbox Code Playgroud)
}
我正在生成的战争叫做:my.app.war.
Tomcat像往常一样从war文件的名称中获取Web上下文根路径,但我不知道在使用基于Java代码的配置时是否会发生更改.
GET http://localhost:8080/my.app/my-resource?level=1
Run Code Online (Sandbox Code Playgroud)
返回404
实际上我通过添加"/"作为@ApplicationPath注释的值来解决这个问题,我认为没有必要,因为API文档为@ApplicationPath值param说了以下内容:
Defines the base URI for all resource URIs. A trailing '/' character will be automatically appended if one is not present.
Run Code Online (Sandbox Code Playgroud)
我假设留下一个空字符串将等同于使用@ApplicationPath("/"),但事实并非如此.
这就是配置类现在的样子:
@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");
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10550 次 |
最近记录: |