S.P*_*.P. 2 jersey filter spring-boot
我是春季和球衣的新人。我正在尝试构建一个过滤器来检查请求是否具有正确的参数。这是我要检查的 json 部分:
"request":{
"application":"1 Android Mobile",
"version":1
}
Run Code Online (Sandbox Code Playgroud)
数据元素是我的 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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的过滤器的代码:
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是从 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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5676 次 |
| 最近记录: |