Jump to content

WebApplicationException and UniformInterfaceException not showing custom message

- - - - -

  • Please log in to reply
9 replies to this topic

#1
javaboss

javaboss

    Newbie

  • Members
  • Pip
  • 5 posts
I am developing a Jersey RESTful service and am having trouble printing out a custom message for my exceptions. My server code is like so:

Response response = null;       


    if (...)

    {

        ...

        response = Response.created(message).entity(message).build();

    }

    else

    {

        response = Response.status(Status.CONFLICT).entity("Message failed to be added.").build();

        throw new WebApplicationException(response);

    }


    return response;

The client code which calls this is like so:

ClientResponse response = resource.post(ClientResponse.class);

    if (response.getStatus() >= 400)

    {

        throw new UniformInterfaceException(response);

    }

My expectations are that in the case of an error the message would be printed to the client's console, but instead it prints: "POST http://localhost:808...jaxrs/messages/ returned a response status of 409." I have tried various things (including head scratching) and have been unsuccessful in finding out why it doesn't work as it should.

Does anyone know how to get this to work?

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Try

throw new UniformInterfaceException(response.getEntity(String.class));



#3
javaboss

javaboss

    Newbie

  • Members
  • Pip
  • 5 posts
It doesn't take just a String as a parameter, but I already tried "throw new UniformInterfaceException(response.getEntity(String.class), response);" which also didn't work.

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Then do

throw new UniformInterfaceException(response.getEntity(String.class)[B], response[/B]);



#5
javaboss

javaboss

    Newbie

  • Members
  • Pip
  • 5 posts
That's what I stated in my reply that I did :).

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Good point :P

#7
javaboss

javaboss

    Newbie

  • Members
  • Pip
  • 5 posts
But what I have noticed (which I didn't before) is that the error returned when I use the two-argument method is: "com.sun.jersey.api.client.ClientHandlerException: java.io.IOException: stream is closed".

#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
What if you comment
throw new WebApplicationException(response);
at the server side then? (I would actually never throw an error in a web service method)

#9
javaboss

javaboss

    Newbie

  • Members
  • Pip
  • 5 posts
I get the same error message as before: "com.sun.jersey.api.client.ClientHandlerExcept ion: java.io.IOException: stream is closed".

The WebApplicationException is supposed to be used in Jersey to allow you to send more meaningful errors to the client, rather than just sending a 500 status error which would be meaningless.

#10
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
This works for me:

Web service class:
package service;


import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;


@Path("testService")
public class WebService {
    @POST
    public Response doSomething(String error) {
        Response response;


        if (!error.equals("error")) {
            response = Response.status(Response.Status.CREATED).entity("This is an awesome message").build();
        } else {
            response = Response.status(Response.Status.CONFLICT).entity("Message failed to be added.").type("text/plain").build();
            throw new WebApplicationException(response);
        }
        return response;
    }
}

web.xml --> I don't think this is used in the test class.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>service</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>
</web-app>

Test:
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.test.framework.JerseyTest;
import org.junit.Test;


import javax.ws.rs.core.MediaType;




public class WebServiceTest extends JerseyTest {


    public WebServiceTest() throws Exception {
        super("service");
    }


    @Test
    public void testHelloWorld() {
        WebResource webResource = resource();
        ClientResponse response = webResource.path("/testService").entity("error").post(ClientResponse.class);
        if (response.getStatus() >= 400) {
            String message = response.getEntity(String.class);
            System.out.println(message);
        }
    }
}

Good luck ;)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users