users@jersey.java.net

Re: [Jersey] Re: JAXBContextResolver not being used

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 27 May 2010 14:01:22 +0200

Hi,

Why don't you use the Jersey client, then you do not have to utilize
BaseJSONMarshaller etc explicitly?

On the client side you register a JAXBContextResolver by adding the
class to a ClientConfig instance (DefaultClientConfig) e.g.

   ClientConfig cc = new DefaultClientConfig();
   cc.getClasses().add(MyResolver.class);

On the server side you register your resolver just like you would a
root resource class. Make sure you annotate your resolver with
@Provider so it gets picked up when scanning.

See the JSON from JAXB sample:

   http://download.java.net/maven/2/com/sun/jersey/samples/json-from-jaxb/1.2/json-from-jaxb-1.2-project.zip

Paul.

On May 27, 2010, at 12:04 PM, vineetkr211 wrote:

>
> I am also facing the same problem. I have deployed Rest services using
> JBoss5.1 and I am calling this rest service using a HTTP client.
> Here is my client code.
>
>
> HttpClient client = new HttpClient();
>
> BufferedReader br = null;
>
> PostMethod method = new
> PostMethod("http://localhost:8080/ResultService/rest/resultservice/
> toc");
> method.addRequestHeader("Content-type", "application/json");
>
>
> com
> .altair
> .hwe.publish.resultservice.schema.resultcommonschema.ObjectFactory
> ob1
> = new
> com
> .altair
> .hwe.publish.resultservice.schema.resultcommonschema.ObjectFactory();
>
>
> com
> .altair
> .hwe.publish.resultservice.schema.tocrequestschema.ObjectFactory
> ob2
> = new
> com
> .altair
> .hwe.publish.resultservice.schema.tocrequestschema.ObjectFactory();
>
> TOCRequest tocRequest = ob2.createTOCRequest();
> // Assume that I have populated tocRequest with proper values.
>
> JAXBContext jc =
> JAXBContext
> .newInstance
> ("com.altair.hwe.publish.resultservice.schema.tocrequestschema");
> BaseJSONMarshaller b = new BaseJSONMarshaller(jc,
> JSONConfiguration.mapped().build());
>
> StringWriter w = new StringWriter();
> b.marshallToJSON(tocRequest, w);
>
> StringRequestEntity ent = new
> StringRequestEntity(w.toString());
> method.setRequestEntity(ent);
>
> try{
> int returnCode = client.executeMethod(method);
> System.out.println("ret: " + returnCode);
> System.out.println(method.getResponseBodyAsString());
>
> }
> catch (Exception e) {
> System.err.println(e);
> } finally {
> method.releaseConnection();
> if(br != null) try { br.close(); } catch (Exception e) {}
> }
>
> Client is generating the correct format. but on server side, all the
> fields
> are set to null.
> Here is my POST method under the rest service:-
>
> @POST
> @Path("/toc")
> @Consumes({"application/json","application/xml"})
> @Produces({"application/json","application/xml"})
> public TOCOfResult getTOCForResultSource(TOCRequest tocRequest)
> throws
> ResultServiceException
> {
> try
> {
> DefaultUserCredentials userCredentials = null;
> try
> {
> userCredentials = new DefaultUserCredentials("user",
> "");
> }
> catch ( Exception e )
> {
> e.printStackTrace();
> }
>
> return m_resultService.getTOC(tocRequest, userCredentials);
> }
> catch ( ResultServiceException e )
> {
> LOGGER.error("Internal error occured. Could not retrieve
> the
> TOC", e);
> throw e;
> }
>
>
> How I can register my JAXBContextResolver class in this case.
>
> Any sort of help would be great.
>
> Thanks in advance.
> --
> View this message in context: http://jersey.576304.n2.nabble.com/JAXBContextResolver-not-being-used-tp2730064p5107836.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>