我正在使用servlet3和jsp处理j2EE项目,并使用restful服务提供应用程序数据。所以我使用以下依赖项来获取我的pom.xml中的restful服务
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.2.1.GA</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>2.2.1.GA</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>2.2.0.GA</version>
</dependency>
这里,我有一个名为MyServices的类文件,它有以下代码,这样就无需在web.xml文件中编写代码来启动restful servlet。
@ApplicationPath("f")
public class MyServices extends Application {
private Set<Object> singletons = new HashSet<Object>();
public MyServices() {
singletons.add(new MyAppServices());
singletons.add(new MyCommonServices());
singletons.add(new MyAddonServices());
}
@Override
public Set<Object> getSingletons() {
return singletons;
}
}
下面,粘贴了在MyServices.Java中启动的所有3个类。
MyAppServices.Java的路径为/ mobapi,可以通过localhost:8080/myapp/mobapi/..
访问
@Path(value = "/mobapi")
public class MyAppServices {
@GET
@Path(value = "/app/{param}")
@Produces(MediaType.APPLICATION_JSON)
public Response getMsg(@PathParam(value = "param") String appId) {
String token = documentDAO.mobileAppToken(appId);
return Response.status(200).entity(token).build();
}
}
MyCommonServices.Java的路径为/,这意味着什么,可以通过localhost:8080/myapp/..
访问
@Path(value = "/")
public class MyCommonServices {
@GET
@Path(value = "/app/{param}")
@Produces(MediaType.APPLICATION_JSON)
public Response getMsg(@PathParam(value = "param") String appId) {
String token = documentDAO.mobileAppToken(appId);
return Response.status(200).entity(token).build();
}
}
MyAddonServices.Java的路径为/ api,可以通过localhost:8080/myapp/api/..
访问
@Path(value = "/api")
public class MyAddonServices {
@GET
@Path(value = "/app/{param}")
@Produces(MediaType.APPLICATION_JSON)
public Response getMsg(@PathParam(value = "param") String appId) {
String token = documentDAO.mobileAppToken(appId);
return Response.status(200).entity(token).build();
}
}
问题是我只能localhost:8080/myapp/app/6546
无问题地访问。
但是,我无法访问其他API,例如localhost:8080/myapp/api/app/6546
或localhost:8080/myapp/mobapi/app/6546
这里是否还有其他任何配置,请问有人帮忙吗?
我使用过滤器来处理像localhost:8080/myApp/app/something
到localhost:8080/myApp/f/app/something
之类的地图请求,如下所示
MyFilter.java
@WebFilter(urlPatterns = "/*")
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (request instanceof HttpServletRequest) {
String path = ((HttpServletRequest) request).getServletPath();
boolean isGet = "GET".equals(((HttpServletRequest) request)
.getMethod());
String newPath = null;
((HttpServletRequest) request).getQueryString();
try {
((HttpServletRequest) request).getQueryString();
if (path.contains("api/")) {
newPath = "f"
+ path
+ "?"
+ ((HttpServletRequest) request)
.getQueryString();
}else {
newPath = "f"
+ path
+ "?"
+ ((HttpServletRequest) request)
.getQueryString();
}
} catch (Exception e) {
newPath = "f" + path;
}
request.getRequestDispatcher(newPath).forward(request, response);
return;
}
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
}
如果我删除此过滤器,我可以访问所有api,而不会出现localhost:8080/myApp/f/app/something
&amp;本地主机:8080 /对myApp / F / API /应用/东西&localhost:8080/myApp/f/mobapi/app/something
有人可以帮助这个过滤器吗?
答案 0 :(得分:0)
尝试这样做:
MyCommonServices.Java
@Path(value = "/app/{param}")
public class MyCommonServices {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getMsg(@PathParam(value = "param") String appId) {
String token = documentDAO.mobileAppToken(appId);
return Response.status(200).entity(token).build();
}
}
MyAppServices.Java
@Path(value = "/mobapi/app/{param}")
public class MyAppServices {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getMsg(@PathParam(value = "param") String appId) {
String token = documentDAO.mobileAppToken(appId);
return Response.status(200).entity(token).build();
}
}
MyAddonServices.Java
@Path(value = "/api/app/{param}")
public class MyAddonServices {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getMsg(@PathParam(value = "param") String appId) {
String token = documentDAO.mobileAppToken(appId);
return Response.status(200).entity(token).build();
}
}
答案 1 :(得分:0)
如果您将MyCommonServices
从@Path(value = "/")
更改为@Path(value = "/app")
和@Path(value = "/app/{param}")
更改为@Path(value = "/{param}")
,该怎么办?