我正在使用带有球衣的dropwizard, 我通过以下方法
定义了ContainerRequestFilter的自定义提供程序import javax.ws.rs.ext.Provider;
import java.io.IOException;
import java.util.List;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.MultivaluedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Provider
public class CustomRequestFilter implements ContainerRequestFilter{
private final static Logger logger = LoggerFactory.getLogger(CustomRequestFilter.class);
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
MultivaluedMap<String,String> myHeaders = requestContext.getHeaders();
List<String> appId = myHeaders.get("myHeader");
logger.info(myHeader.get(0));
logger.info("Hello Word, it is customRequestFilter");
}
}
但系统正在抛出错误
ERROR [2014-05-26 10:04:46,556] com.sun.jersey.server.impl.container.filter.FilterFactory: The filter, of type myPackage.CustomRequestFilter, MUST be of the type String, String[], Class<? extends
com.sun.jersey.spi.container.ContainerRequestFilter>, or an instance of
com.sun.jersey.spi.container.ContainerRequestFilter. The filter is ignored.
答案 0 :(得分:-1)
我认为这只是使用正确进口的问题。试试这个:
import java.util.List;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerRequestFilter;
@Provider
public class CustomRequestFilter implements ContainerRequestFilter {
private final static Logger logger = LoggerFactory.getLogger(CustomRequestFilter.class);
public ContainerRequest filter(ContainerRequest requestContext) {
MultivaluedMap<String,String> myHeaders = requestContext.getRequestHeaders();
List<String> appId = myHeaders.get("myHeader");
logger.info(appId.get(0));
logger.info("Hello Word, it is customRequestFilter");
return requestContext;
}
}