gpt4 book ai didi

spring-boot - 如何从 ContainerRequestContext 获取数据构建 DTO 对象

转载 作者:行者123 更新时间:2023-12-04 03:03:17 24 4
gpt4 key购买 nike

我是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com