使用Dropwizard的Jersey过滤器,拦截器和MessageBodyReader
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Dropwizard的Jersey过滤器,拦截器和MessageBodyReader相关的知识,希望对你有一定的参考价值。
我正在一个dropwizard项目中探索球衣过滤器。尝试探索如何在公共场所为每个端点拦截传入的请求,以对请求对象(例如,路径参数,查询参数或请求主体)执行一些基本检查我正在尝试遵循界面
- 过滤器
- ContainerRequestFilter
- ReaderInterceptor
- MessageBodyReader
我能够实现Filter和ContainerRequest Filter,并查看这些实现中的代码正在执行。但是我的ReaderInterceptor和MessageBodyReader没有得到执行。
这是我的代码
PersonResource.java
@Path("person")
@Api("Person Resource")
@Log
@MyAnnotation
public class PersonResource
@GET
@Path("id")
@Produces(MediaType.APPLICATION_JSON)
public Person getPerson(@PathParam("id") String id)
log.info("Requesting Person information...");
return new Person(id,"Person Name");
过滤器实现
package com.practice.rigz.interceptor;
import lombok.extern.java.Log;
import javax.servlet.*;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
@Provider
@Log
public class RequestFilter implements Filter
@Override
public void init(FilterConfig filterConfig) throws ServletException
log.info("INIT...");
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
filterChain.doFilter(servletRequest,servletResponse);
log.info("DO FILTER...");
@Override
public void destroy()
log.info("DESTROY...");
ContainerRequestFilter实现
package com.practice.rigz.interceptor;
import com.practice.rigz.annotations.MyAnnotation;
import lombok.extern.java.Log;
import javax.ws.rs.container.*;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
import java.util.List;
@Provider
@Log
@MyAnnotation
public class RequestContainerRequestFilter implements ContainerRequestFilter
@Override
public void filter(ContainerRequestContext requestContext) throws IOException
log.info(String.join("," ,requestContext.getPropertyNames()));
List<String> partnerId = requestContext.getUriInfo().getPathParameters().get("id");
if (partnerId.get(0).equals("1"))
throw new IllegalAccessError();
log.info("CONTAINER REQUEST FILTER...");
阅读器拦截器实现
package com.practice.rigz.interceptor;
import com.practice.rigz.annotations.MyAnnotation;
import lombok.extern.java.Log;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.ext.ReaderInterceptor;
import javax.ws.rs.ext.ReaderInterceptorContext;
import java.io.IOException;
@Log
@MyAnnotation
@Provider
public class RequestReaderInterceptor implements ReaderInterceptor
@Override
public Object aroundReadFrom(ReaderInterceptorContext readerInterceptorContext) throws IOException, WebApplicationException
log.info("INTERCEPTOR");
return readerInterceptorContext.proceed();
MessageBodyReader实现
package com.practice.rigz.interceptor;
import com.practice.rigz.annotations.MyAnnotation;
import lombok.extern.java.Log;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
@Provider
@Log
@MyAnnotation
public class RequestMessageBodyReader implements MessageBodyReader
@Override
public boolean isReadable(Class aClass, Type type, Annotation[] annotations, MediaType mediaType)
log.info("IS READABLE...");
return false;
@Override
public Object readFrom(Class aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap multivaluedMap, InputStream inputStream) throws IOException, WebApplicationException
log.info("READ FROM ...");
return null;
并且在我的Dropwizard Application运行方法中,我正在如下注册所有组件
private void registerFilters(Environment environment)
environment.servlets().addFilter("request-filter", RequestFilter.class)
.addMappingForUrlPatterns(java.util.EnumSet.allOf(javax.servlet.DispatcherType.class), true, "/*");
environment.jersey().register(RequestMessageBodyReader.class);
environment.jersey().register(RequestReaderInterceptor.class);
environment.jersey().register(RequestContainerRequestFilter.class);
知道我的MessageBodyReader和ReaderInterceptor为什么行不通吗?我正在使用OpenJDK11和Dropwizard 1.3.16
以下是尝试到达终点时我程序的输出
INFO [2019-11-23 16:29:07,016] com.practice.rigz.interceptor.RequestContainerRequestFilter:org.glassfish.jersey.message.internal.TracingLoggerINFO [2019-11-23 16:29:07,019] com.practice.rigz.interceptor.RequestContainerRequestFilter:容器请求过滤器...INFO [2019-11-23 16:29:07,045] com.practice.rigz.resources.Person资源:请求人信息...INFO [2019-11-23 16:29:07,119] com.practice.rigz.interceptor.RequestFilter:做过滤器...0:0:0:0:0:0:0:1--[23 / Nov / 2019:16:29:07 +0000]“ GET / person / 1111 HTTP / 1.1” 200 34“ http://localhost:8080/swagger”“ Mozilla / 5.0(Macintosh;英特尔Mac OS X 10_14_6)AppleWebKit / 605.1.15(Khtml,例如Gecko)版本/13.0.3 Safari / 605.1.15“ 242
我发现了问题的原因。我需要的一些东西在这里
- 拦截器适用于POST方法。
- MessageBodyReader也适用于Post方法,因为只有POST才能包含主体。我总是从isReadableMethod返回false。如果我希望我的实现处理特定类型的请求,则此方法应该返回true。将实现更改为以下
package com.practice.rigz.interceptor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import com.practice.rigz.annotations.MyAnnotation;
import com.practice.rigz.model.Person;
import lombok.extern.java.Log;
import org.hibernate.validator.internal.util.privilegedactions.NewJaxbContext;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Link;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
@Provider
@Log
@MyAnnotation
public class RequestMessageBodyReader implements MessageBodyReader<Person>
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
log.info("IS READABLE...");
return type == Person.class;
@Override
public Person readFrom(Class<Person> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException
ObjectMapper mapper = new ObjectMapper();
Person person = mapper.readValue(entityStream, Person.class);
log.info("READ FROM...");
return person;
以上是关于使用Dropwizard的Jersey过滤器,拦截器和MessageBodyReader的主要内容,如果未能解决你的问题,请参考以下文章
在 dropwizard 0.7.0 中使用 CORS 标头过滤器
在 APIHealthCheck 上获取 Dropwizard 客户端和 Jersey/HTTP I/O 错误