URL of a Jersey Application using ResourceConfig without web.xml

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

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.

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> 

My configuration class that extends ResourceConfig is:

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");     } } 

one of my resources is:

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; } 

}

The war that I am generating is called: my.app.war.

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 

Returns a 404

回答1:

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. 

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");     } } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!