Quantcast

JAXBContextResolver not being used

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

JAXBContextResolver not being used

Chad McHenry
A JAXBContextResolver I created is not being used, and my Resources are producing JSON output which looks like "mapped" builder rather than from the "natural" builder I expect. There are no errors at runtime.

How should my JAXBContextResolver be found by Jersey? It seems I have not configured something correctly, as it is never instantiated, nor is getContext() invoked.

Is there something special I need to do in spring or jersey to get this @Provider recognized?

I am using jersey-1.0.2 with spring-2.5.6 (and tried with jersey-1.0.3 with the same results).

@Provider
public final class JAXBContextResolver implements ContextResolver<JAXBContext> {

    private final JAXBContext context;
    private final Set<Class<?>> types;
    private final Class<?>[] cTypes = { MyClass.class }
    
    public JAXBContextResolver() throws Exception {
        // logging shows this is never executed
        this.types = new HashSet<Class<?>>(Arrays.asList(cTypes));
        this.context = new JSONJAXBContext(JSONConfiguration.natural().build(), cTypes);
    }
    
    public JAXBContext getContext(Class<?> objectType) {
        // logging shows this is never executed
        return (types.contains(objectType)) ? context : null;
    }
}

...Chad
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JAXBContextResolver not being used

Paul Sandoz
Administrator

On Apr 28, 2009, at 6:43 AM, Chad McHenry wrote:

> A JAXBContextResolver I created is not being used, and my Resources  
> are producing JSON output which looks like "mapped" builder rather  
> than from the "natural" builder I expect. There are no errors at  
> runtime.
>
> How should my JAXBContextResolver be found by Jersey?

The same why that root resource classes are found e.g. declare the  
package where JAXBContextResolver resides or make it a Spring bean.


> It seems I have not configured something correctly, as it is never  
> instantiated, nor is getContext() invoked.
>
> Is there something special I need to do in spring or jersey to get  
> this @Provider recognized?
>

What is the package of JAXBContextResolver and what is your web.xml  
configuration?


> I am using jersey-1.0.2 with spring-2.5.6 (and tried with  
> jersey-1.0.3 with the same results).
>

You could register JAXBContextResolver as a singleton spring bean e.g.  
annotate with spring annotation @Component

Paul.

> @Provider
> public final class JAXBContextResolver implements  
> ContextResolver<JAXBContext> {
>
>     private final JAXBContext context;
>     private final Set<Class<?>> types;
>     private final Class<?>[] cTypes = { MyClass.class }
>
>     public JAXBContextResolver() throws Exception {
>         // logging shows this is never executed
>         this.types = new HashSet<Class<?>>(Arrays.asList(cTypes));
>         this.context = new  
> JSONJAXBContext(JSONConfiguration.natural().build(), cTypes);
>     }
>
>     public JAXBContext getContext(Class<?> objectType) {
>         // logging shows this is never executed
>         return (types.contains(objectType)) ? context : null;
>     }
> }
>
> ...Chad


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JAXBContextResolver not being used

Paul Sandoz
Administrator
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: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JAXBContextResolver not being used

vineetkr211
Thanks for your reply.

I have one more question. Is it possible to send plain json string to my rest web service and read it as jaxb type on the server. Actually my client is a javascript based thats why I am asking this.

Also my jaxb types are schema aware, so I will have to send my json string in BadgerFish format or I can use other Builders as well?

Thanks once again
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JAXBContextResolver not being used

Paul Sandoz
Administrator

On May 28, 2010, at 10:06 PM, vineetkr211 wrote:

>
> Thanks for your reply.
>
> I have one more question. Is it possible to send plain json string  
> to my
> rest web service and read it as jaxb type on the server. Actually my  
> client
> is a javascript based thats why I am asking this.
>

Yes, as long as the "on-the-wire" format is JSON and of the  
appropriate form.


> Also my jaxb types are schema aware, so I will have to send my json  
> string
> in BadgerFish format or I can use other Builders as well?
>

You need to ensure the client is in sync with the convention defined  
on the server side.

This is one of the issues with the JAXB/JSON integration as it is  
often harder to know what the required structure of the JSON should be  
than say using Jackson, which has a more direct mapping between Java  
objects and the JSON serialization.

Paul.

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Loading...