gpt4 book ai didi

java - 如何将 Java 对象序列化为 JSON 并将其返回到 servlet 过滤器中?

转载 作者:搜寻专家 更新时间:2023-11-01 03:56:36 24 4
gpt4 key购买 nike

我有这个 javax.servlet.Filter 来检查是否允许客户端访问 API REST 资源。

@Component
public class AuthorizationRequestFilter implements Filter {

public static final String AUTHORIZATION_TOKEN = "X-Access-Token";

@Autowired
@Qualifier("loginService")
private ILoginService loginService;

private void throwUnauthorized(ServletResponse res) throws IOException {

HttpServletResponse response = (HttpServletResponse) res;

response.reset();
response.setHeader("Content-Type", "application/json;charset=UTF-8");
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);

}

private void throwForbidden(ServletResponse res) throws IOException {

HttpServletResponse response = (HttpServletResponse) res;

response.reset();
response.setHeader("Content-Type", "application/json;charset=UTF-8");
response.sendError(HttpServletResponse.SC_FORBIDDEN);

}

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) req;

String accessToken = request.getHeader(AUTHORIZATION_TOKEN);

if (StringUtils.isEmpty(accessToken)) {
throwUnauthorized(res);
} else {
AccountLoginData account = loginService.find(accessToken);
if (account == null) {
throwForbidden(res);
}
}

chain.doFilter(req, res);

}

@Override
public void destroy() {
}

@Override
public void init(FilterConfig arg0) throws ServletException {
}

}

它有效,但我想在这两个 throw*() 方法中使用适当的信息写入客户端 JSON。在这个应用程序的另一部分,我使用这些响应消息对象来通知客户端发生了什么。

比如没有找到记录时:

public class NotFoundResponseMessage extends ResponseMessage {

public NotFoundResponseMessage(String message) {
super(HttpStatus.NOT_FOUND, 1, message);
}

}

public class ResponseMessage {

private int status;
private int code;
private String message;
private String reason;

public ResponseMessage(int status, int code, String message, String reason) {

Assert.notNull(reason, "Reason must not be null.");
Assert.isTrue(status > 0, "Status must not be empty.");

this.status = status;
this.code = code;
this.message = message;
this.reason = reason;

}

我的问题

我想在我的 javax.servlet.Filter 授权/身份验证过滤器中返回带有序列化对象(UnauthorizedResponseMessageForbiddenResponseMessage)的 JSON。我使用 Spring Boot 和 Jackson 库。

  1. 如何手动ResponseMessage 序列化为它的 JSON 表示形式?
  2. 如何在我的过滤器类中将此 JSON 写回给客户端?

编辑 1:

private void throwUnauthorized(ServletResponse res) throws IOException {

HttpServletResponse response = (HttpServletResponse) res;

response.reset();
response.setHeader("Content-Type", "application/json;charset=UTF-8");
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.getWriter().write("{\"foo\":\"boo\"}");

}

现在我可以写出 JSON 但返回 HTTP 500,因为:

java.lang.IllegalStateException: getWriter() has already been called for this response
at org.apache.catalina.connector.Response.getOutputStream(Response.java:544)

最佳答案

使用JacksonObject转换为JSON,下面是一个例子

ObjectMapper mapper = new ObjectMapper();
String Json = mapper.writeValueAsString(object);

关于java - 如何将 Java 对象序列化为 JSON 并将其返回到 servlet 过滤器中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32584733/

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