如何以空实体返回响应状态405? [英] how can i return response status 405 with empty entity?

查看:134
本文介绍了如何以空实体返回响应状态405?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Java REST中返回带有空实体的响应状态405?

How can i return response status 405 with empty entity in java REST?

@POST
@Path("/path")
public Response createNullEntity() {
    return Response.created(null).status(405).entity(null).build();
}

它返回状态码405,但实体不为null,它是错误405的http页面.

It returns status code 405, but the entity is not null, it is the http page for the error 405.

推荐答案

返回错误状态时,Jersey通过 Java Servlet规范中概述了此过程. a>§10.9错误处理.

When you return an error status, Jersey delegates the response to your container's error processing via sendError. When sendError is called, the container will serve up an error page. This process is outlined in the Java Servlet Specification §10.9 Error Handling.

我怀疑您看到的是405响应的容器默认错误页面.您可以通过指定自定义错误页面来解决问题(可以为空).另外,如果您在响应中提供实体,则Jersey将不使用sendError.您可以给它一个空字符串,如下所示:

I suspect what you are seeing is your container's default error page for a 405 response. You could probably resolve your issue by specifying a custom error page (which could be empty). Alternatively, Jersey won't use sendError if you provide an entity in your response. You could give it an empty string like this:

@POST
@Path("/path")
public Response createNullEntity() {
  return Response.status(405).entity("").build();
}

上面的结果在0Content-Length

这篇关于如何以空实体返回响应状态405?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆