如何在 Jersey 的 MessageBodyWriter 中更改 HTTP 状态?
我知道我可以通过 writeTo 方法中的 httpHeaders 和 entityStream 更改 header 和正文响应消息,但我不知道如何更改 HTTP 状态。

@Provider
@Produces(MediaType.APPLICATION_JSON)
public class MessageBodyWriterJSON implements MessageBodyWriter<Object> {

    @Override
    public void writeTo(Object t, Class<?> type, Type genericType,
        Annotation[] annotations, MediaType mediaType,
        MultivaluedMap<String, Object> httpHeaders,
        OutputStream entityStream) throws IOException,
        WebApplicationException {
        // ...???
    }

}

最佳答案

您可以通过抛出 WebApplicationException 来更改 http 状态。

Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
throw new WebApplicationException(response);

关于java - 是否可以更改 MessageBodyWriter 中的 HTTP 状态?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16736745/

10-12 00:37
查看更多