users@jersey.java.net

[Jersey] Re: can't get FEATURE_POJO_MAPPING working (spring, jersey, grizzly)

From: Gonfi den Tschal <gonfidentschal_at_gmail.com>
Date: Wed, 22 Feb 2012 17:08:09 +0200

the manual at http://jersey.java.net/nonav/documentation/latest/json.html
for "5.1. POJO support" doesn't say that a custom provider needs to be
registered.


On Wed, Feb 22, 2012 at 5:03 PM, Gonfi den Tschal
<gonfidentschal_at_gmail.com> wrote:
> no, didn't.
>
> now i've added the class:
>
> @Provider
> public class MyObjectMapperProvider implements ContextResolver<ObjectMapper> {
>
>    final ObjectMapper defaultObjectMapper;
>
>    public MyObjectMapperProvider() {
>        defaultObjectMapper = createDefaultMapper();
>    }
>
>    @Override
>    public ObjectMapper getContext(Class<?> type) {
>        return defaultObjectMapper;
>    }
>
>    private static ObjectMapper createDefaultMapper() {
>        ObjectMapper result = new ObjectMapper();
>        result.configure(Feature.INDENT_OUTPUT, true);
>        return result;
>    }
> }
>
> i can't register it as in the example since i don't have an "Application".
> instead i have a ResourceConfig, and i tried:
>
> rc.getProviderClasses().add(MyObjectMapperProvider.class);
>
> but that didn't change anything.
>
>
>
>
> On Wed, Feb 22, 2012 at 4:09 PM, Glen Mazza <gmazza_at_talend.com> wrote:
>> Did you link in something similar to the MyObjectMapperProvider class as
>> given in the jacksonjsonprovider sample?
>> (http://java.net/projects/jersey/sources/svn/content/trunk/jersey/samples/jacksonjsonprovider/src/main/java/com/sun/jersey/samples/jacksonjsonprovider/MyApplication.java?rev=4816)
>>  That might be the issue.
>>
>> Glen
>>
>>
>> On 02/21/2012 08:40 PM, Gonfi den Tschal wrote:
>>>
>>> having troubles getting marshalling to work.
>>>
>>> setup:
>>>  - spring
>>>  - jersey
>>>  - grizzly
>>>
>>>
>>> maven dependencies:
>>>
>>>         <dependency>
>>>             <groupId>com.sun.jersey.contribs</groupId>
>>>             <artifactId>jersey-spring</artifactId>
>>>             <version>1.11</version>
>>>             <exclusions>
>>>                 <exclusion>
>>>                     <groupId>org.springframework</groupId>
>>>                     <artifactId>spring</artifactId>
>>>                 </exclusion>
>>>                 <exclusion>
>>>                     <groupId>org.springframework</groupId>
>>>                     <artifactId>spring-core</artifactId>
>>>                 </exclusion>
>>>                 <exclusion>
>>>                     <groupId>org.springframework</groupId>
>>>                     <artifactId>spring-web</artifactId>
>>>                 </exclusion>
>>>                 <exclusion>
>>>                     <groupId>org.springframework</groupId>
>>>                     <artifactId>spring-beans</artifactId>
>>>                 </exclusion>
>>>                 <exclusion>
>>>                     <groupId>org.springframework</groupId>
>>>                     <artifactId>spring-context</artifactId>
>>>                 </exclusion>
>>>             </exclusions>
>>>         </dependency>
>>>         <dependency>
>>>             <groupId>com.sun.jersey</groupId>
>>>             <artifactId>jersey-json</artifactId>
>>>             <version>1.11</version>
>>>         </dependency>
>>>         <dependency>
>>>             <groupId>com.sun.jersey</groupId>
>>>             <artifactId>jersey-grizzly2</artifactId>
>>>             <version>1.11</version>
>>>         </dependency>
>>>
>>> starting the server:
>>>             ResourceConfig rc = new DefaultResourceConfig();
>>>             rc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,
>>> Boolean.TRUE);
>>>             IoCComponentProviderFactory factory = new
>>> SpringComponentProviderFactory(rc, context);
>>>             try {
>>>                 GrizzlyServerFactory.createHttpServer(getBaseUri(),
>>> rc, factory);
>>>             } catch (IOException e) {
>>>                 throw new RuntimeException("Failed starting REST web
>>> server!", e);
>>>             }
>>>
>>> the service:
>>>
>>> @Service
>>> @Path("/ping")
>>> @Produces({"application/json"})
>>> public class PingService {
>>>
>>>     @GET @Path("/foo")
>>>     public String foo(@QueryParam("bar") final Bar bar) {
>>>     }
>>>
>>> }
>>>
>>> sending a type String instead of Bar works fine. as soon as i send
>>> objects i get all kinds of errors.
>>>
>>> bar class:
>>>
>>> public class Bar {
>>>
>>>     private String field1;
>>>     private String field2;
>>>
>>>     public String getField1() {
>>>         return field1;
>>>     }
>>>     public void setField1(String field1) {
>>>         this.field1 = field1;
>>>     }
>>>
>>>     public String getField2() {
>>>         return field2;
>>>     }
>>>     public void setField2(String field2) {
>>>         this.field2 = field2;
>>>     }
>>> }
>>>
>>> with this setup, when starting the server, i get:
>>>
>>>   SEVERE: Missing dependency for method public java.lang.String
>>> mypackage.PingService.foo(mypackage.Bar) at parameter at index 0
>>>   SEVERE: Method, public java.lang.String
>>> mypackage.PingService.foo(mypackage.Bar), annotated with GET of
>>> resource, class mypackage.PingService, is not recognized as valid
>>> resource method.
>>>
>>>
>>> if i annotate the Bar class with @XmlRootElement (which i don't want,
>>> because i cannot touch the classes i would need to transfer) then the
>>> server starts, but calling the method results in a 404 even though the
>>> get request looks fine.
>>>
>>> most examples out there just use strings as inputs.
>>>
>>> been trying for hours, giving up. any idea what else i should check or
>>> try or read?
>>
>>
>>
>> --
>> Glen Mazza
>> Talend Community Coders
>> coders.talend.com
>> blog: www.jroller.com/gmazza
>>