I'm experimenting with Jersey 2.17 trying to create RESTful Web Services, I read the Jersey User guide 2.17 and followed the instructions in chapter 10 to create filters, my Problem is that the filter is working perfectly when the resource is called from a browser, however when I call the resource from a java SE test client the filter does not get triggered. ,the filter is PreMatching ContainerRequestFilter, do you have any idea if there is an extra setting required to enable the filter across the whole resource, I mean make it work even if the call is made form a test client instead of a browser.
This is by requestFilter
package com.vogella.jersey.fist;
import java.io.IOException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.Response;
@PreMatching
public class RequestFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext arg0) throws IOException {
System.out.println(" Media Type : "+arg0.getMediaType());
System.out.println("Method Type : "+arg0.getMethod());
System.out.println("Method length : "+arg0.getLength());
System.out.println("request time : "+arg0.getDate());
System.out.println(" message : "+arg0.toString());
arg0.abortWith(Response
.status(Response.Status.UNAUTHORIZED)
.entity("User cannot access the resource.")
.build());
}
}
and this is myApplicationFile :
package com.vogella.jersey.fist;
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
@ApplicationPath("/")
public class MyApplication extends ResourceConfig {
public MyApplication() {
// Resources.
packages(Hello.class.getPackage().getName());
// register(LoggingFilter.class);
register(RequestFilter.class);
// register(ResponseFilter.class);
}
}
and finally this is my 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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>com.vogella.jersey.first</display-name>
<servlet>
<servlet-name>MyApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Register resources and providers under com.vogella.jersey.first package. -->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.vogella.jersey.fist</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>com.vogella.jersey.fist.RequestFilter</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyApplication</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>][2]
below is my Test Client Code .
ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient(config);
// client.register(MyJAXBContextProvider.class);
WebTarget service = client.target(UriBuilder.fromUri("http://localhost:8080/com.vogella.jersey.fist").build());
System.out.println(service.path("rest").path("todo").request().accept(MediaType.TEXT_HTML).get(String.class));
Todo a = service.path("rest").path("todo").request().accept(MediaType.APPLICATION_XML).get(Todo.class) ;
System.out.println(a.getDescription());
Can anybody tell me how to get past that problem? PS : I'm using TomCat8 as a server.
答案 0 :(得分:0)
好的,所以在我的RequestFilter类(在@PreMathcing之前)添加@Provider anottation解决了我的问题。
谢谢你peeskillet