How to use Jersey interceptors to get request body(如何使用 Jersey 拦截器获取请求正文)
问题描述
我在我的项目中使用 REST-Jersey
.所有 POST 数据都以 JSON
格式发送,并在服务器端解组到相应的 bean 中.像这样的:
I am using REST-Jersey
in my project. All the POST data is send in JSON
format and unmarshalled at server-side into respective beans. Something like this:
向服务器发送请求:
$('a#sayHelloPost').click(function(event){
event.preventDefault();
var mangaData = {
title:'Bleach',
author:'Kubo Tite'
}
var formData=JSON.stringify(mangaData);
console.log(formData);
$.ajax({
url:'rest/cred/sayposthello',
type: 'POST',
data: formData,
dataType: 'json',
contentType:'application/json'
})
});
有效载荷:
{"title":"Bleach","author":"Kubo Tite"}
服务器端:
@POST
@Path("/sayposthello")
@Produces(MediaType.APPLICATION_JSON)
public Response sayPostHello(MangaBean mb){
System.out.println(mb);
return Response.status(200).build();
}
漫画豆:
public class MangaBean {
private String title;
private String author;
@Override
public String toString() {
return "MangaBean [title="ICsgdGl0bGUgKyA=", author=" + author + "]";
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
控制台输出:
MangaBean [title=Bleach, author=Kubo Tite]
我从 这里获得了 REST-interceptor 实现.
public class JerseyFilter implements ContainerRequestFilter{
@Override
public ContainerRequest filter(ContainerRequest req) {
return req;
}
}
我想访问拦截器中的有效负载(请求正文).由于数据为 JSON 格式,因此无法作为请求参数访问.有没有办法在拦截器方法中获取请求体?请指教.
I want to access the payload(request body) in the interceptor. As the data is in JSON format, its not accessible as request parameters. Is there a way I can get the request body in the interceptor method? Please advice.
推荐答案
这是您可以实现的一种方式,与 Jersey 实现其日志过滤器的方式非常相似.您可以读取实体并将其粘贴到请求中,这样您就不会意外地在过滤器中使用它.
Here is one way you can implement, very similar to how Jersey implements its logging filters. You can read the entity and stick it back to the request, so you accidentally do not consume it in your filter.
import com.sun.jersey.api.container.ContainerException;
import com.sun.jersey.core.util.ReaderWriter;
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerRequestFilter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class JerseyFilter implements ContainerRequestFilter {
@Override
public ContainerRequest filter(ContainerRequest request) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = request.getEntityInputStream();
final StringBuilder b = new StringBuilder();
try {
if (in.available() > 0) {
ReaderWriter.writeTo(in, out);
byte[] requestEntity = out.toByteArray();
printEntity(b, requestEntity);
request.setEntityInputStream(new ByteArrayInputStream(requestEntity));
}
return request;
} catch (IOException ex) {
throw new ContainerException(ex);
}
}
private void printEntity(StringBuilder b, byte[] entity) throws IOException {
if (entity.length == 0)
return;
b.append(new String(entity)).append("
");
System.out.println("#### Intercepted Entity ####");
System.out.println(b.toString());
}
}
这篇关于如何使用 Jersey 拦截器获取请求正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 Jersey 拦截器获取请求正文
- 如何使用WebFilter实现授权头检查 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01