1

In my JAXWS web service I need to send a specific message back to the client when an exception occurs, not the standard Fault message with the description of the exception.

How can this be done?

I am using jaxws-rt version 2.1.3

I have already tried to use exception mappers like this but without luck (some how they are not called, this could also be caused by a mistake in the configuration):

@Provider
public class ThrowableExceptionMapper implements ExceptionMapper<Throwable> {

    public ThrowableExceptionMapper() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public Response toResponse(Throwable throwable) {
        if (throwable instanceof WebApplicationException) {
            return ((WebApplicationException) throwable).getResponse();
        } else {
            return Response.serverError().entity("").build();
        }
    }
}

The server we use is JBoss EAP 6.4.

Edit:
The exception mapper approach is not suited for my JAXWS web service, because this is for JAX-RS (thanks SRINIVAS K). Is there something similar available for JAXWS?

Patrick Koorevaar
  • 1,219
  • 15
  • 24

3 Answers3

1

I managed to rewrite the response message with the help of this page:
http://www.java-tips.org/java-ee-tips-100042/156-java-api-for-xml-web-services/1958-writing-a-handler-in-jax-ws.html

And with some examples of this page:
http://www.programcreek.com/java-api-examples/index.php?api=javax.xml.ws.handler.MessageContext

I put together this class:

public class MyLogicalHandler implements LogicalHandler<LogicalMessageContext> {
    private final String RejectionResponseBody = "<ns2:MessageControllerResponse xmlns:ns2=\"http://some.namespace.com/\"><return>SOMEDATA</return></ns2:MessageControllerResponse>";

    @Override
    public boolean handleMessage(LogicalMessageContext context) {       
        return true;
    }

    @Override
    public boolean handleFault(LogicalMessageContext context) {
        processMessage(context);
        return true;
    }

    @Override
    public void close(MessageContext context) {

    }

    private void processMessage(LogicalMessageContext context) {
        Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

        if (outboundProperty) {
            LogicalMessage msg = context.getMessage();
            msg.setPayload(new StreamSource(new ByteArrayInputStream(RejectionResponseBody.getBytes())));
        }
    }   
}

Edit, additional information added:

You also need to add the HandlerChain annotation to the web service:

...
@HandlerChain(file = "handler-chain.xml")
public class MyWebService {
...
}

And create a handler-chain xml file:

<?xml version="1.0" encoding="UTF-8"?>
<handler-chains xmlns="http://java.sun.com/xml/ns/javaee">  
  <handler-chain>
    <handler>
      <handler-class>my.package.ws.jaxws.MyLogicalHandler</handler-class>
    </handler>
  </handler-chain>
</handler-chains>

And place this file in your resources folder of the web service, you need to use the same package structure as you did with the web service. So create the following package: my.package.ws in the resources folder if your web service class resides in the my.package.ws package.

Patrick Koorevaar
  • 1,219
  • 15
  • 24
0

Can you try using below?

String errorMessage = "this is custom error message";
return Response.status(e.getResponse().getStatus()).entity(errorMessage).build(); 
Garry
  • 4,493
  • 3
  • 28
  • 48
  • Thanks, but in the current situation the mapper is not called. I placed a break point on the first if statement in the toResponse Method but this is never hit. So I think it must be a problem with the configuration or I do it wrongly. – Patrick Koorevaar Jul 08 '15 at 06:41
0

Can you check if ExceptionMapper works with generic exceptions like Throwable or Exception.
I see some examples with custom defined exceptions that are configured with WebFault annotation.

kswaughs
  • 2,967
  • 1
  • 17
  • 21
  • I got this idea from here: http://stackoverflow.com/questions/19621653/how-should-i-log-uncaught-exceptions-in-my-restful-jax-rs-web-service Next to the throwable exception mapper I also made some dedicated Exception Mappers for the exceptions: PersistenceException, IllegalArgumentException and EJBTransactionRolledbackException. But these are not called either. – Patrick Koorevaar Jul 08 '15 at 07:11
  • Can you give me a link/example of how they do this with the WebFault annotation? – Patrick Koorevaar Jul 08 '15 at 07:12
  • the exceptions that you mentioned are not custom/user defined exceptions, they are framework related exceptions, Hope this link might be useful. https://blog.idrsolutions.com/2013/10/web-services-exception-handling/ – kswaughs Jul 08 '15 at 07:34
  • one more observation, the link that you shared is related to JAX-RS RESTful service, Your problem describes about exceptions in JaxWS web service. – kswaughs Jul 08 '15 at 07:39
  • Ah ok, thanks. That makes sense, that explains why it did not work. I need to look for something similar in JAXWS then. Thanks for the link, it is not useful for the problem in this question but maybe I can make use of it later (when I need to handle custom exceptions). And yes the exceptions for now are framework related exceptions, but they result in a soap response message with a element. This is what I want to customize by sending a response message as would be sent if there was not an exception encountered on the server. – Patrick Koorevaar Jul 08 '15 at 08:18