java过滤器只过滤json请求,如何将Java对象序列化为JSON并在servlet过滤器中返回?...

I have this javax.servlet.Filter to check whether client is allowed to access API REST resource.

@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 {

}

}

it works but I would like to in these two throw*() methods write to the client JSON with appropriate information. In another part of this application I use these response message objects to inform client what happened.

For example, when record has not been found:

public class NotFoundResponseMessage extends ResponseMessage {

public NotFoundResponseMessage(String message) {

super(HttpStatus.NOT_FOUND, 1, message);

}

}

and

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;

}

}

My Question

I would like to return JSON with serialized objects (UnauthorizedResponseMessage and ForbiddenResponseMessage) in my javax.servlet.Filter authorization / authentication filter. I use Spring Boot and Jackson library.

How can I manually serialize ResponseMessage into its JSON representation?

How can I write out this JSON back to the client in my filter class?

Edit 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\"}");

}

Now I can write out JSON but HTTP 500 is returned, because:

java.lang.IllegalStateException: getWriter() has already been called for this response

at org.apache.catalina.connector.Response.getOutputStream(Response.java:544)

解决方案

Using Jackson convert Object to JSON, the following is an example

ObjectMapper mapper = new ObjectMapper();

String Json = mapper.writeValueAsString(object);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值