cat*_*tox 4 java rest web-services jersey
起初在我的 Web 服务器中,我只有一个 REST servlet。就像是:
@Path("/")
public class Controller {
@GET
@Produces({ MediaType.TEXT_HTML })
public Response get(@Context UriInfo info) throws Exception {
...
}
@GET
@Path("resource1")
@Produces({ MediaType.TEXT_HTML })
public Response resource1() throws Exception {
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
和 web.xml:
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>xpto.mypack1;xpto.mypack2</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
但是后来我想向服务器添加一些静态 html,所以我将 servlet 映射更新为 /rest/*
和控制器 servlet 类的 @Path 指令从“/”到“/rest”。一切正常,但带有@path 指令的控制器的子资源或方法停止工作.. 即:
有什么帮助吗?我已经尝试了每个@Path 指令之后和之前的 / 组合列表,但没有成功......非常感谢
一更新:
我使用了 trace util 并得到了以下结果:
对于 /[app-name]/rest(它有效):
对于 /[app-name]/rest/resource1(它不起作用):
我希望它可以帮助某人帮助我..
如果您将 servlet 映射定义为/rest/*,请不要/rest在@Path资源的注释中重复。即您需要做的就是保持控制器原样(在您上面的问题中),只需更改 servlet 映射即可。资源可用的 URL 是:
<application_context_path>/<servlet_mapping>
Run Code Online (Sandbox Code Playgroud)
因此,如果您将@Path注释从更改@Path("/")为@Path("rest")并且还将 servlet 映射更改为/rest,那么您的资源将在以下位置可用:
<application_context_path>/rest/rest/*
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9169 次 |
| 最近记录: |