简体   繁体   English

JAX-RS:用于基本HTTP身份验证的REST客户端

[英]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. 我需要使用JAX-RS的Jersey实现编写一个REST客户端,以访问与安全EJB一起使用的RESTful Web服务。 I have a @OneToMany relationship from class A to B . 我从类AB有一个@OneToMany关系。 I am using Glassfish 3.1.1 application server. 我正在使用Glassfish 3.1.1应用程序服务器。 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是我的其余客户端,我在一个独立的客户端应用程序中使用它来调用已实现为EJB会话Bean的RESTful Web服务。

MyRestClient.java 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是我的REST Web服务,已实现为EJB Web服务。

AResource.java 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. AB是用于保存从客户端服务器返回的数据的域对象。

A.java 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 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. 谢谢。

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). 您在客户端遇到的第一个异常是由以下事实造成的:服务器上存在错误,并且服务器返回了意外的响应(因为它代替了资源响应,而是返回了带有HTML正文的错误响应)。

The second problem is caused by a Jersey's 1.8 hard dependency on JAXB RI, which got introduced by mistake. 第二个问题是由于泽西岛对JAXB RI的1.8硬依赖性而引起的,这是错误引入的。 So, when you try using MOXy, it would fail. 因此,当您尝试使用MOXy时,它将失败。 This is fixed in Jersey 1.9 and 1.9.1. 此问题在Jersey 1.9和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. 因此,如果您想继续使用MOXy,只需将GF中的Jersey从更新中心升级到最新版本,它就可以开始工作了。

I was using org.eclipse.persistence.moxy-2.3.jar to test it over JAXB RI used by Glassfish 3.1.1. 我正在使用org.eclipse.persistence.moxy-2.3.jar通过Glassfish 3.1.1使用的JAXB RI org.eclipse.persistence.moxy-2.3.jar进行测试。 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. 我已经使用我的域对象在类路径中的jaxb.properties文件中设置了javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory来测试MOXy,该MOXy在删除后解决了问题。

So, removing the jaxb.properties file from classpath solved the problem. 因此,从类路径中删除jaxb.properties文件解决了该问题。

The problem was being created by the line: 该问题是由以下行造成的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM