2

How can i access and configure objectmapper in resteasy?

Im aware similar question has been asked and answered multiple times. eg.: Accessing Jackson Object Mapper in RestEasy , http://wiki.fasterxml.com/JacksonFAQJaxRs

However my application is just an jse app, it contains no web.xml file. How can i use my own provider/modify objectmapper.

Community
  • 1
  • 1
hnnn
  • 504
  • 2
  • 7
  • 24

1 Answers1

1

You can do this with a JAXRS Application and overriding getSingletons. You can do this outside the container. No web.xml required.

import javax.ws.rs.core.Application;

@ApplicationPath("/rest")
public class ResourceConfiguration extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<>();
        resources.add(//Your Class decorated with @Path
        return resources;
    }

    @Override
    public Set<Object> getSingletons() {
        Set<Object> s = new HashSet<Object>();

        JacksonJaxbJsonProvider jaxbProvider = new JacksonJaxbJsonProvider();
        jaxbProvider.setMapper(mapper);

        s.add(jaxbProvider);
        return s;
    }
}
rjdkolb
  • 10,377
  • 11
  • 69
  • 89
  • Thank you for the reply. I found someting similar here https://docs.jboss.org/resteasy/docs/1.0.1.GA/userguide/html/Installation_Configuration.html but this approach suggest me to register this class into context-param section which is part of web.xml. Also i forgot to write im using only resteasy client. – hnnn Jan 20 '16 at 07:34
  • @hnnn , correct. I assumed you extend an Application to get this working in Java SE ? Just by overriding getSingletons() it will work – rjdkolb Jan 20 '16 at 07:37
  • Nope it doesnt work. I dont have any component autoscan avalaible. I need to register the jackson provider/applicatin manually . I tried to register the provider into ResteasyProviderFactory and ResteasyClientBuilder but netiher has worked. – hnnn Jan 20 '16 at 07:53
  • @hnnn I fixed the application to override getClasses so you don't need auto scanning. This should work. I use it in production ;-) – rjdkolb Jan 20 '16 at 08:08