自定义响应HTTP 状态?

发布于 2024-08-25 19:00:49 字数 726 浏览 9 评论 0原文

我的项目有一个休息界面。 对于一个类,我有一个 POST 方法,您可以在其中发布 xml,然后我返回一个自定义响应,例如:

无效电子邮件

如果电子邮件来自发布的 xml 不正确+我为不同情况定义的其他自定义消息。

对于所有这些,HTTP 状态会自动设置为 200(正常)。 有什么办法可以改变吗?

Ps:我知道我可以抛出一个网络应用程序,例如:

throw new WebApplicationException(Response.Status.BAD_REQUEST);

但在这种情况下,我的自定义响应不再包含在内。

所以我只想返回我的自定义错误 + 400 作为 http 响应。

提前致谢。

评论后更新: 我的方法是:

 @POST
 @Path("{membershipExternalId}")
 @Consumes(MediaType.APPLICATION_XML)
 @Produces("application/xml")
 public CustomResponse invite(){ //code}

你看到我返回了我的自定义响应。如果我返回简单的响应,我可以设置状态,但在这种情况下我看不到任何方法。

I have a rest interface for my project.
For one class i have a POST method where you can post an xml and i RETURN a custom response like:

<customResponse>Invalid email</customResponse>

if the email from the xml which was posted, was incorrect + other custom messages i have defined for different situations.

For all of these the HTTP STATUS is automatically put on 200 (OK).
Is there any way to change it?

Ps: I know that i can throw a web application like :

throw new WebApplicationException(Response.Status.BAD_REQUEST);

but in this case my custom response is no more included.

So i just want to return my custom error + 400 as http response.

Thanks in advance.

UPDATE after comments:
My method is:

 @POST
 @Path("{membershipExternalId}")
 @Consumes(MediaType.APPLICATION_XML)
 @Produces("application/xml")
 public CustomResponse invite(){ //code}

You see that i return my CUSTOM RESPONSE. If i would return simple RESPONSE i could set the STATUS but in this case i cannot see any way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鹊巢 2024-09-01 19:00:49

找到解决方案:

将返回类型作为方法的响应:

     @POST
     @Path("{membershipExternalId}")
     @Consumes(MediaType.APPLICATION_XML)
     @Produces("application/xml")
     public Response invite(){ //code

     if (fail())
        return Response.status(400).entity(customResponse).build();
}

Response.status(400).entity(customResponse) 就可以了。当 build() 时,它将转换您的自定义响应 xml =>

HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
X-Powered-By: Servlet 2.4; JBoss-4.2.3.GA (build: SVNTag=JBoss_4_2_3_GA date=200807181439)/JBossWeb-2.0
Set-Cookie: JSESSIONID=1C72921619A6B32BC1166B3567A39ADA; Path=/
Content-Type: application/xml
Content-Length: 140
Date: Thu, 18 Mar 2010 12:15:15 GMT
Connection: close

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><customResponse><message>Invalid email</message></customResponse>

Found the solution:

Put the return type as Response to the method:

     @POST
     @Path("{membershipExternalId}")
     @Consumes(MediaType.APPLICATION_XML)
     @Produces("application/xml")
     public Response invite(){ //code

     if (fail())
        return Response.status(400).entity(customResponse).build();
}

Response.status(400).entity(customResponse) will do the trick. When build() it will convert your custom response xml =>

HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
X-Powered-By: Servlet 2.4; JBoss-4.2.3.GA (build: SVNTag=JBoss_4_2_3_GA date=200807181439)/JBossWeb-2.0
Set-Cookie: JSESSIONID=1C72921619A6B32BC1166B3567A39ADA; Path=/
Content-Type: application/xml
Content-Length: 140
Date: Thu, 18 Mar 2010 12:15:15 GMT
Connection: close

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><customResponse><message>Invalid email</message></customResponse>
云胡 2024-09-01 19:00:49

setStatus or sendError on HttpServletResponse should do the trick.

输什么也不输骨气 2024-09-01 19:00:49

这是标记为 Java 的,但我无法识别 Response.Status.BAD_REQUEST。

对于 Java,只需在 HttpServletResponse 对象上调用 setStatus 即可。

对于 .NET,它看起来像这样:

HttpContext.Current.Response.StatusCode = xxx;

This is tagged Java but I don't recognize Response.Status.BAD_REQUEST.

For Java just call setStatus on the HttpServletResponse object.

For .NET it looks like this:

HttpContext.Current.Response.StatusCode = xxx;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文