- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Spring 和 Jersey 新人。我正在尝试构建一个过滤器来检查请求是否具有正确的参数。这是我要检查的 json 部分:
"request":{
"application":"1 Android Mobile",
"version":1
}
数据的元素是我的VersionDTO的一部分:
public class VersionDTO {
int version;
String application;
public String getApplication() {
return application;
}
public void setApplication(String application) {
this.application = application;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
}
这是我的过滤器的代码:
@Override
public void filter(ContainerRequestContext requestContext) throws FilterException {
if (isSecureURL(requestContext.getUriInfo().getPath())){}{
try {
versionFilterService.setVersionDTO(//here is where I want to pass the VersionDTO object with the data of the requestContext);
} catch (ServiceException e) {
e.printStackTrace();
}
}
}
private boolean isSecureURL(String url) {
return !UnSecureUrlEnum.isUnSecureUrl(url);
}
}
我想做的是从 ContainerRequestContext 获取数据并构建一个 VersionDTO 以将对象传递给我的服务。这可能吗??
提前致谢!
最佳答案
是的,这是可能的。使用 Jersey,您可以在读取流之前对其进行缓冲,从而允许再次读取流。您需要转换到 Jersey ContainerRequest
,您可以在其中 bufferEntity()
然后简单地 readEntity
来获取反序列化的对象。
@Override
public void filter(ContainerRequestContext requestContext) throws FilterException {
ContainerRequest cr = (ContanerRequest) requestContext;
cr.bufferEntity();
VersionDTO dto = cr.readEntity(VersionDTO.class);
}
关于spring-boot - 如何从 ContainerRequestContext 获取数据构建 DTO 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40841709/
在我的 Jersey 应用程序中,我希望将一个 ContainerRequestContext 实例注入(inject)到各种对象中。如果对象是在请求上下文之外创建的,我希望注入(inject) nu
如何从 ContainerRequestContext 对象获取引用 URL。 请找到下面的代码片段, public void filter(ContainerRequestContext reque
这是我的身份验证过滤器: @Secured @Provider @Priority(Priorities.AUTHENTICATION) public class SecuredFilter impl
我使用 Jersey 2 来公开功能(restful API)。 我想在过滤器级别访问表单参数的地方,我们从 `ContainerRequestContext 访问 pathparam、querypa
我想为我的 REST API 端点应用授权过滤器,过滤器需要路径参数来进行过滤。这是我的端点和代码: 端点: curl --url 'localhost:80/reports/resources/or
我已经看过很多帖子,但似乎没有什么能像我喜欢的那样工作。 我想将一个对象从过滤器注入(inject)到 ContainerRequestContext 属性中,并稍后在其他类中检索它。 这是我的过滤器
我是 Spring 和 Jersey 新人。我正在尝试构建一个过滤器来检查请求是否具有正确的参数。这是我要检查的 json 部分: "request":{ "application":
我的请求中有以下 HTTP header ,我想从中提取 JSESSIONID: Accept=application/json Accept-Encoding=gzip deflate,Accept
我尝试使用 Mockito 模拟 ContainerRequestContext,但出现以下错误: java.lang.ClassFormatError: Absent Code attribute
我有一个 RESTful 端点,我通过自定义 ContainerRequestFilter 通过简单的授权检查来保护它。过滤器检查 HTTP session 中包含的所有信息是否正确,如果不正确,则执
我有一个过滤器,POST REST api 通过该过滤器,我想在过滤器中提取我的有效负载的以下部分。 { "foo": "bar", "hello": "world" } 过滤
我的代码是这样的: public void filter(ContainerRequestContext request) throws IOException { // can I get
我使用 IOUtils.toString(containerRequestContext.getEntityStream(),"UTF-8");获取实体流并使用 IOUtils 将其转换为字符串。但是
我是一名优秀的程序员,十分优秀!