users@jersey.java.net

[Jersey] Re: Own _at_Provider with Jackson INDENT_OUTPUT

From: Simon Herter <simon.herter_at_student.kit.edu>
Date: Tue, 14 Jan 2014 16:33:30 +0100

Hi Michal,

that does help! Thank you so much for your effort.

Simon



Am 14.01.2014 15:43, schrieb Michal Gajdos:
> Hi Simon,
>
> found a bug in Jersey that causes your problem (see [1]). There is a
> workaround however - register JacksonJsonProvider ([2]) in your app as
> well and your ObjectMapper should be used:
>
> packages("edu.kit.tm.cm.sc");
>
> register(JsonProvider.class);
> register(JacksonJsonProvider.class);
>
>
> [1] https://java.net/jira/browse/JERSEY-2335
> [2]
> https://github.com/FasterXML/jackson-jaxrs-providers/blob/master/json/src/main/java/com/fasterxml/jackson/jaxrs/json/JacksonJsonProvider.java
>
> HTH,
> Michal
>
> On 14.01.2014, 01:03 , Simon Herter wrote:
>> Hi Michal,
>>
>> thank you for your response. I followed your instructions and removed
>> the dependency on "jersey-media-json-jackson".
>> Unfortunately that doesn't change anything. It seems that the default
>> Jackson provider is still used and not my own one.
>>
>> I pushed my changes, could you have another look at it?
>> https://github.com/ne0h/proseminar2013/tree/jsonprovider/server/Java/SmartCampusServer/
>>
>>
>> With best thanks
>> Simon
>>
>> Am 13.01.2014 22:21, schrieb Michal Gajdos:
>>> Hi Simon,
>>>
>>> you have dependency on both Jackson 1.x (via jersey-media-json-jackson)
>>> and Jackson 2.x (directly in your pom.xml). ObjectMapper class doesn't
>>> seem to be in the same package in Jackson 1
>>> (org.codehaus.jackson.map.ObjectMapper) than it is in Jackson 2 (
>>> com.fasterxml.jackson.databind.ObjectMapper). You have created
>>> ContextResolver for the latter (Jackson 2) but it's possible that
>>> Jackson 1 is used to (un)marshall your entities (and ContextResolver is
>>> not found). Try to remove dependency on jersey-media-json-jackson (+
>>> remove registration of JacksonFeature) and run your app again (MBR/MBW
>>> from Jackson 2 will still get registered via META-INF/services
>>> mechanism).
>>>
>>> Michal
>>>
>>> On 13.01.2014, 19:59 , Simon Herter wrote:
>>>> Hi,
>>>>
>>>> I have some problems when using my own @Provider. I'm using Jackson
>>>> for JSON processing and all works well but I can't figure out, how to
>>>> modify the jackson content provider.
>>>>
>>>> I registered my own provider as shown in documentation
>>>> (https://jersey.java.net/documentation/latest/media.html#json.jackson)
>>>> but the output does not change.
>>>>
>>>>
>>>> @Provider
>>>> @Produces(MediaType.APPLICATION_JSON)
>>>> public class JsonProvider implements ContextResolver<ObjectMapper>{
>>>>
>>>> final ObjectMapper om;
>>>>
>>>> public JsonProvider() {
>>>> om = new ObjectMapper();
>>>> om.enable(SerializationFeature.INDENT_OUTPUT);
>>>> }
>>>>
>>>> @Override
>>>> public ObjectMapper getContext(Class<?> type) {
>>>> return om;
>>>> }
>>>> }
>>>>
>>>>
>>>>
>>>> @ApplicationPath("/")
>>>> public class App extends ResourceConfig {
>>>>
>>>> public SmartCampusServer() {
>>>> packages("package");
>>>> register(JsonProvider.class);
>>>> register(JacksonFeature.class);
>>>> }
>>>> }
>>>>
>>>> I'm using recent stable versions of jackson and jersey.
>>>> Full Code is available as maven project on github
>>>> https://github.com/ne0h/proseminar2013/tree/jsonprovider/server/Java/SmartCampusServer/
>>>>
>>>>
>>>> Simon Herter
>>>>
>>>
>>
>