skip
skip

Reputation: 12683

JAX-RS: REST Client for Basic HTTP Authentication

I need to write a REST client with Jersey implementation of JAX-RS to access my RESTful webservice that works with a secure EJB. I have a @OneToMany relationship from class A to B. I am using Glassfish 3.1.1 application server. But when I run the my rest client it gives me following errors:

Error on the client side:

com.sun.jersey.api.client.UniformInterfaceException: GET http://localhost:8080/myapp/rest/a/all returned a response status of 500 Internal Server Error

Error on the server side:

SEVERE: The response of the WebApplicationException cannot be utilized as the response is already committed. Re-throwing to the HTTP container
javax.ws.rs.WebApplicationException: javax.xml.bind.PropertyException: Unsupported Property
    at com.sun.jersey.core.provider.jaxb.AbstractListElementProvider.writeTo(AbstractListElementProvider.java:183)
    at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1437)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:619)

MyRestClient is my rest client that I am using in a standalone client application to call my RESTful webservice that I have implemented as an EJB session bean.

MyRestClient.java

public class MyRestClient {
    public static void main(String[] args) {
        Client client = Client.create();        
        client.addFilter(new HTTPBasicAuthFilter("username", "password"));
        WebResource resource = client.resource("http://localhost:8080/myapp/rest/a/all");
        List<A> aList = resource.get(new GenericType<List<A>>() {});
        for(A nextA: aList){
            System.out.println(nextA.getTitle());
        }
    }
}

AResource is my REST webservice that has been implemented as an EJB webservice.

AResource.java

@Stateless
@Path("/a")
public class AResource {

    @EJB
    private AService aService;

    @GET
    @Produces(MediaType.APPLICATION_XML)
    @Path("/all")
    public List<A> getAllA() {
        return aService.getAllA();
    }
}

A and B are the domain objects used to hold the data returned from the server at the client side.

A.java

@XmlRootElement
public class A implements Serializable{ 

    private List<B> bList = new ArrayList<B>();
    public List<B> getBList() {
        return bList;
    }
    //remaining code

}

B.java

@XmlRootElement
public class B implements Serializable {

    private String text;
    private A a;    


    @XmlTransient
    public A getA() {
        return a;
    }

    public void afterUnmarshal(Unmarshaller u, Object parent) {
        this.a = (A) parent;
    }
    //remaining code

}

Could someone help me understand why am I getting these errors?

Thanks.

Upvotes: 0

Views: 6753

Answers (2)

Martin Matula
Martin Matula

Reputation: 7989

The first exception you were getting on the client side is caused by the fact there was an error on the server and the server returned unexpected response (as instead of the resource response, it returned an error response with html body).

The second problem is caused by a Jersey's 1.8 hard dependency on JAXB RI, which got introduced by mistake. So, when you try using MOXy, it would fail. This is fixed in Jersey 1.9 and 1.9.1. So, if you want to continue using MOXy, just upgrade Jersey in GF from update center to the latest one and it should start working.

Upvotes: 1

skip
skip

Reputation: 12683

I was using org.eclipse.persistence.moxy-2.3.jar to test it over JAXB RI used by Glassfish 3.1.1. I had set javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory in jaxb.properties file in the classpath with my domain objects to test MOXy, which after being removed solved the problem.

So, removing the jaxb.properties file from classpath solved the problem.

The problem was being created by the line:

List<A> aList = resource.get(new GenericType<List<A>>() {});

Upvotes: 0

Related Questions