0

I've a SOAP web service built in Java.

If my method runs into an exception I want to return a "HTTP CODE 500".

Is it possible? If yes how?

(Web service is running on Tomcat 6)

seafoxx
  • 1,731
  • 1
  • 13
  • 17
Danny.
  • 363
  • 1
  • 4
  • 23

2 Answers2

2

maybe you should simply throw a qualified Exception yourself which then will be sent back to the client as a soap fault. W3C tells us this:

In case of a SOAP error while processing the request, the SOAP HTTP server MUST issue an HTTP 500 "Internal Server Error" response and include a SOAP message in the response containing a SOAP Fault element (see section 4.4) indicating the SOAP processing error. http://www.w3.org/TR/2000/NOTE-SOAP-20000508/

Messing with http response codes could be dangerous as some other client might expect a different response. In your case you'd be lucky because you want exactly the the behaviour as specified by W3C. So throw an Exception ;)

How to do that? Take a look here: How to throw a custom fault on a JAX-WS web service?

Greetings

Bastian

Community
  • 1
  • 1
seafoxx
  • 1,731
  • 1
  • 13
  • 17
  • maybe this answer is also helpful, the question was "SOAP faults or results object?": http://stackoverflow.com/a/3087265/1075628 – seafoxx Oct 23 '13 at 20:07
  • thanks so far. but you written in the comment below "uld run into problems. "Hey, You've got some problems but I won't share the details..." "... . That's what I want ! The client doesn't care about any exception details. He just want to know "error or no error". – Danny. Oct 24 '13 at 07:26
  • Hey Dan, then simply throw an empty Exception which results in a Fault. You do not need to share information about the problem itself, but it might help the client to decide what to do with the problem. As long as you stick to SOAP, use Faults (and get the return code 500 for free ;) – seafoxx Oct 25 '13 at 13:41
1

Since the JAX-WS is based on servlets, you can do it. You can try the next:

@WebService
public class Calculator {

    @Resource
    private WebServiceContext ctx;

    public int division (int a, int b) {
        try {
            return a / b;
        } catch (ArithmeticException e) {
            sendError(500, "Service unavailable for you.");
            return -1; // never send
        }
    }

    private void sendError(int status, String msg) {
        try {
            MessageContext msgCtx = ctx.getMessageContext();
            HttpServletResponse response = 
               (HttpServletResponse) msgCtx.get(MessageContext.SERVLET_RESPONSE);
            response.sendError(status, msg);
        } catch (IOException e) {
            // Never happens or yes?
        }
    }

}

However, I prefer to use JAX-RS to do something similar.

@PUT
@Path("test")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response update( //
        @FormParam("id") int id,
        @FormParam("fname") String fname,
        @FormParam("lname") String lname
        ) {
    try {

        // do something

        return Response.ok("Successfully updated", 
                MediaType.TEXT_PLAIN_TYPE).build();
    } catch (Exception e) {
        LOG.error("An error occurred", e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
                .entity("An error occurred")
                .type(MediaType.TEXT_PLAIN_TYPE).build();
    }
}
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
  • correct for JAX-RS, where you are encouraged to take full advantage of all HTTP methods and response codes as you prefer. For JAX-WS there are a lot more restrictions and you should avoid to interfere with the http level diretcly. Your example would (at least I think so) send a HTTP Response 500, but no SOAP FAULT as response body, thus any valid JAX-WS-client could run into problems. "Hey, You've got some problems but I won't share the details..." – seafoxx Oct 23 '13 at 19:24