User Response + HTTP Status?

I have an interface for my project. For one class, I have a POST method where you can send xml and I RETURN a custom answer , for example:

<customResponse>Invalid email</customResponse>

if the xml email that was posted was incorrect + other custom posts that I defined for different situations.

For all of these conditions, HTTP STATUS is automatically set to 200 (OK). Is there any way to change it?

Ps: I know that I can drop a web application like:

throw new WebApplicationException(Response.Status.BAD_REQUEST);

but in this case my user response is no longer included.

So, I just want to return my custom + 400 error as a response to http.

Thanks in advance.

UPDATE after comments: My method:

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

, . RESPONSE, STATUS, .

+3
3

:

:

     @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>
+8

setStatus sendError HttpServletResponse .

+2

Java, Response.Status.BAD_REQUEST.

Java setStatus HttpServletResponse.

.NET :

HttpContext.Current.Response.StatusCode = xxx;

0

Source: https://habr.com/ru/post/1737511/


All Articles