users@jersey.java.net

[Jersey] Re: posting or putting an object

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Thu, 15 Sep 2011 14:01:03 +0200

Hi John,

please see inline...

On 14.9.2011 17:40, John Okrasa wrote:
>
> Hello is there a way of passing an object (which contains a list) as
> an input to a Jersey WS
>

Yes, the above is possible. The code you posted is a bit tricky to
check, as it contains
a lot of other (than Jersey related) functionality and too much
commented-out code,
while the JAXB classes are missing.

You may try this:

server:

     @XmlRootElement
     public class ListBean {
         public List<String> list = new LinkedList<String>();
     }

     @POST
     @Consumes("application/xml")
     @Produces("application/xml")
     public ListBean post(ListBean lb) {
         return lb;
     }

    client:

         ListBean lb = new ListBean();
         lb.list.add("one");
         ListBean res = webResource
                                         .accept("application/xml")
                                         .type("application/xml")
                                         .post(ListBean.class, lb);


> //_at_POST
>
> //_at_Produces(value = "application/xml")
>
> @POST
>
> @Consumes(value = "application/xml")
>
> @Produces(value = "application/xml")
>
> public CPC3Output lookupDispenserInfo(CPC3Input cpc3Input) {
>
> //inject remote services
>
> ServiceInjector.injectServicesToTarget(this);
>
> //open a session in database to link transaction log ot
> modifications
>
> RamqSession001Ouput session =
> ramqSession001Service.createAuthenticationSession();
>
>
> cpc3Input.setAuthenticationSessionId(session.getAthenticationSessionId());
>
> cpc3Input.setLogicalSessionId(session.getLogicalSessionId());
>
> CPC3Output cpc3Output =
> cpc3DispenserInfoService.doLookupDispenserInfo(cpc3Input);
>
> //close the session in database
>
>
> ramqSession001Service.closeAuthenticationSession(session.getLogicalSessionId());
>
> return cpc3Output;
>
> }
>
> --------------------------------------------------------------------------------------------------------------------------------------------
>
> public static CPC3Output lookupDispenserInfo(CPC3Input cpc3Input) {
>
> //LOG.info("Calling RAMQ deconsent with: PHYSCODE={} | NAM={}
> | DECONSENT_MODE={}", new Object[]{ physcode, patientNam,
> deconsentMode });
>
> //assume failure
>
> CPC3Output cpc3Output = new CPC3Output();
>
> cpc3Output.setSuccess(false);
>
> try {
>
> WebResource resource =
> Client.create().resource(RamqBridgeClientProperties.RAMQ_RWS_DISPENSERINFO_URL);
>
>
> resource.type(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML);
>
> resource.post(cpc3Input);
>
> //*
>
> //TODO: find a way to pass CPC3Input to the WebService
>
> cpc3Output = resource.get(CPC3Output.class);
>
> // ClientResponse.Status status =
> response.getClientResponseStatus();
>
> // if (status.equals(ClientResponse.Status.OK)) {
>
> // cpc3Output = response.getEntity(new
> GenericType<CPC3Output>() {});
>
> // //LOG.debug("Received {} consent success flag from
> RAMQ web service", cpc3Output.getSuccess(),
> response.getClientResponseStatus().getStatusCode());
>
> // } else {
>
> // //LOG.warn("RAMQ web service returned status {} -
> {}", status.getStatusCode(), status.getReasonPhrase());
>
> // }
>
> // cpc3Output.setSuccess(true);
>
> //*/
>
> } catch (ClientHandlerException ex) {
>
> LOG.error("Failed to lookup dispenser information", ex);
>
> }
>
> return cpc3Output;
>
> results in....
>
> *com.sun.jersey.api.client.**UniformInterfaceException:*POST
> http://localhost:6080/ramq/ramq-ws/dispenserInfo returned a *response
> status of 500*
>
> I'malsotrying what's described in Example 4.6 in Section 4.3 of the
> Jersey 1.9 User guide...
>
> //JAXBElement<CPC3Input> JAXBin = new JAXBElement(new
> QName("/dispenserInfo"),CPC3Input.class,cpc3Input);
>
> JAXBElement<CPC3Input> JAXBin = new JAXBElement(new
> QName("licenseNo"),CPC3Input.class,cpc3Input);
>
> CPC3Output cpc3Output =
> cpc3DispenserInfoService.doLookupDispenserInfo(JAXBin);
>
> Which is to post a JAXBElement<MyObj> ..
>
> This is not working.... It gives error..
>

Please check the Jersey jaxb example out (see [1]). The JAXBElement use
case is covered there.

HTH,

~Jakub

[1]https://maven.java.net/content/repositories/releases/com/sun/jersey/samples/jaxb/1.9.1/jaxb-1.9.1-project.zip

> SEVERE: Failed to lookup dispenser information
>
> com.sun.jersey.api.client.ClientHandlerException:
> javax.ws.rs.WebApplicationException: javax.xml.bind.MarshalException
>
> at
> org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1027)
>
> at
> org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:137)
>
> at
> org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:121)
>
> at org.testng.TestRunner.runWorkers(TestRunner.java:1030)
>
> at org.testng.TestRunner.privateRun(TestRunner.java:709)
>
> at org.testng.TestRunner.run(TestRunner.java:579)
>
> at org.testng.SuiteRunner.runTest(SuiteRunner.java:331)
>
> at
> org.testng.SuiteRunner.runSequentially(SuiteRunner.java:326)
>
> at org.testng.SuiteRunner.privateRun(SuiteRunner.java:288)
>
> at org.testng.SuiteRunner.run(SuiteRunner.java:193)
>
> at
> org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:910)
>
> at org.testng.TestNG.runSuitesLocally(TestNG.java:879)
>
> at org.testng.TestNG.run(TestNG.java:787)
>
> at
> org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:75)
>
> at
> org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:92)
>
> Caused by: javax.ws.rs.WebApplicationException:
> javax.xml.bind.MarshalException
>
> - with linked exception:
>
> [java.net.ConnectException: Connection refused: connect]
>
> at
> com.sun.jersey.core.provider.jaxb.AbstractJAXBElementProvider.writeTo(AbstractJAXBElementProvider.java:136)
>
> at
> com.sun.jersey.core.provider.jaxb.AbstractJAXBElementProvider.writeTo(AbstractJAXBElementProvider.java:79)
>
> at
> com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:311)
>
> at
> com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:181)
>
> at
> com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:129)
>
> ... 27 more
>
> Caused by: javax.xml.bind.MarshalException
>
> - with linked exception:
>
> [java.net.ConnectException: Connection refused: connect]
>
> at
> com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:319)
>
> at
> com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:243)
>
> at
> javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:74)
>
> at
> com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider.writeTo(XMLJAXBElementProvider.java:118)
>
> at
> com.sun.jersey.core.provider.jaxb.AbstractJAXBElementProvider.writeTo(AbstractJAXBElementProvider.java:134)
>
> ... 31 more
>
> Caused by: java.net.ConnectException: Connection refused: connect
>
> at java.net.PlainSocketImpl.socketConnect(Native Method)
>
> at
> java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
>
> at
> java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
>
> at
> java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
>
> at
> java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
>
> at java.net.Socket.connect(Socket.java:529)
>
> at java.net.Socket.connect(Socket.java:478)
>
> at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
>
> at
> sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
>
> at
> sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
>
> at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
>
> at sun.net.www.http.HttpClient.New(HttpClient.java:306)
>
> at sun.net.www.http.HttpClient.New(HttpClient.java:323)
>
> at
> sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:970)
>
> at
> sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)
>
> at
> sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)
>
> at
> sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1014)
>
> at
> com.sun.jersey.client.urlconnection.URLConnectionClientHandler$1$1.getOutputStream(URLConnectionClientHandler.java:202)
>
> at
> com.sun.jersey.api.client.CommittingOutputStream.commitWrite(CommittingOutputStream.java:117)
>
> at
> com.sun.jersey.api.client.CommittingOutputStream.write(CommittingOutputStream.java:89)
>
> at
> com.sun.xml.internal.bind.v2.runtime.output.UTF8XmlOutput.flushBuffer(UTF8XmlOutput.java:390)
>
> at
> com.sun.xml.internal.bind.v2.runtime.output.UTF8XmlOutput.endDocument(UTF8XmlOutput.java:121)
>
> at
> com.sun.xml.internal.bind.v2.runtime.XMLSerializer.endDocument(XMLSerializer.java:820)
>
> at
> com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.postwrite(MarshallerImpl.java:368)
>
> at
> com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:315)
>
> ... 35 more
>
> *From:*John Okrasa
> *Sent:* Sunday, September 11, 2011 9:57 PM
> *To:* Jonathan Richard
> *Subject:* CPC3 passing object
>
> Good morning Jonathan,
>
> Hope you had a good weekend.
>
> I guess you know the WS will accept an object... so I am trying..
>
> //_at_POST
>
> //_at_Produces(value = "application/xml")
>
> @POST
>
> @Consumes(value = "application/xml")
>
> @Produces(value = "application/xml")
>
> public CPC3Output lookupDispenserInfo(CPC3Input cpc3Input) {
>
> //inject remote services
>
> ServiceInjector.injectServicesToTarget(this);
>
> //open a session in database to link transaction log ot
> modifications
>
> RamqSession001Ouput session =
> ramqSession001Service.createAuthenticationSession();
>
>
> cpc3Input.setAuthenticationSessionId(session.getAthenticationSessionId());
>
> cpc3Input.setLogicalSessionId(session.getLogicalSessionId());
>
> CPC3Output cpc3Output =
> cpc3DispenserInfoService.doLookupDispenserInfo(cpc3Input);
>
> //close the session in database
>
>
> ramqSession001Service.closeAuthenticationSession(session.getLogicalSessionId());
>
> return cpc3Output;
>
> }
>
> --------------------------------------------------------------------------------------------------------------------------------------------
>
> public static CPC3Output lookupDispenserInfo(CPC3Input cpc3Input) {
>
> //LOG.info("Calling RAMQ deconsent with: PHYSCODE={} | NAM={}
> | DECONSENT_MODE={}", new Object[]{ physcode, patientNam,
> deconsentMode });
>
> //assume failure
>
> CPC3Output cpc3Output = new CPC3Output();
>
> cpc3Output.setSuccess(false);
>
> try {
>
> WebResource resource =
> Client.create().resource(RamqBridgeClientProperties.RAMQ_RWS_DISPENSERINFO_URL);
>
>
> resource.type(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML);
>
> resource.post(cpc3Input);
>
> //*
>
> //TODO: find a way to pass CPC3Input to the WebService
>
> cpc3Output = resource.get(CPC3Output.class);
>
> // ClientResponse.Status status =
> response.getClientResponseStatus();
>
> // if (status.equals(ClientResponse.Status.OK)) {
>
> // cpc3Output = response.getEntity(new
> GenericType<CPC3Output>() {});
>
> // //LOG.debug("Received {} consent success flag from
> RAMQ web service", cpc3Output.getSuccess(),
> response.getClientResponseStatus().getStatusCode());
>
> // } else {
>
> // //LOG.warn("RAMQ web service returned status {} -
> {}", status.getStatusCode(), status.getReasonPhrase());
>
> // }
>
> // cpc3Output.setSuccess(true);
>
> //*/
>
> } catch (ClientHandlerException ex) {
>
> LOG.error("Failed to lookup dispenser information", ex);
>
> }
>
> return cpc3Output;
>
> results in....
>
> *com.sun.jersey.api.client.**UniformInterfaceException:*POST
> http://localhost:6080/ramq/ramq-ws/dispenserInfo returned a *response
> status of 500*
>
> **
>
> John Okrasa
>
> Java Developer
>
> Clinical and Health Informatics Research Group
>
> 1140 Pine Ave. West Montreal QC H3A 1A3
>
> TEL: 514-934-1934, x32975
>
> FAX: 514-843-1551
>
> john.okrasa_at_mail.mcgill.ca <mailto:john.okrasa_at_mail.mcgill.ca>
>