0

During Jersey Upgrade lot of apis from com.sun.jersey is not supported in the Jersey 2.29 which supports Java11. What should be the alternatives for below APIs ?

ServletHolder sh = new ServletHolder(ServletContainer.class);
            sh.setInitParameter("com.sun.jersey.config.property.resourceConfigClass",
            "com.sun.jersey.api.core.PackagesResourceConfig");
            sh.setInitParameter(PackagesResourceConfig.PROPERTY_PACKAGES,
                    String.format("%s;%s", ThrowableMapper.class.getPackage().getName(), packageName));
            sh.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", Boolean.TRUE.toString());
ServletContextHandler context = new ServletContextHandler();
            if (contextPath.indexOf("/") == 0) {
                contextPath = contextPath.substring(1);
            }
            context.setContextPath(String.format("/api/mid/%s", contextPath));
            context.addServlet(sh, "/*");
  • Did you check the official migration guide: https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/migration.html? – zhulien Jan 25 '21 at 09:48
  • [Here is an example](https://stackoverflow.com/a/28734049/2587435) – Paul Samsotha Jan 26 '21 at 17:23
  • @zhulien : Migration guide is not that helpful. It's too abstract :( – Abhishek Kumar Jan 28 '21 at 14:00
  • @PaulSamsotha : That has a good set, I am wondering if to enable `POJOMappingFeature` in Jersey 2.9 we can just add ` jersey-media-json-jackson` extension module. – Abhishek Kumar Jan 28 '21 at 14:05
  • You don't need that (POJO mapping feature) in Jersey 2.x. If you are using 2.9, also register the JacksonFeature with the ResourceConfig. If you use 2.29, you don't need to. Make sure you remove all the Jersey 1.x dependencies. And yes, just adding jersey-media-json-jackson is all you need for Jackson support. – Paul Samsotha Jan 28 '21 at 14:07
  • Thanks @PaulSamsotha. Is there any user guide which says that adding extension `jersey-media-json-jackson` would auto discover JacksonFeatures from 2.29 onwards ? – Abhishek Kumar Jan 29 '21 at 06:58
  • 1
    Thanks @zhulien for pointing towards the guide. – Abhishek Kumar Jan 29 '21 at 06:59
  • In the user guide they talk about autodiscoverables. From 2.9 or 2.10 on this module added the Jackson autodiscoverable. Look maybe chapter 4 in user guide – Paul Samsotha Jan 29 '21 at 08:21

1 Answers1

1
("com.sun.jersey.api.json.POJOMappingFeature", Boolean.TRUE.toString()); 

is used to provide a POJO Support whereas in 2.x, Jersey has few of the auto discoverable features like JSON Binding where we just have to add the dependency in the pom so that they get loaded in the classpath.

("com.sun.jersey.config.property.resourceConfigClass","com.sun.jersey.api.core.PackagesResourceConfig")    

is used to create an instance of resourceConfigClass. Although in few of the forums it is mentioned that even if it's not instantiated it would create an instance on a fly to add resources. Now in 2.x if ServerProperties.PROVIDER_PACKAGES is used then source code of ServletContainer also confirms the same so we should not need to instantiate resourceConfigClass separately. So, this parameter can easily be removed.

(PackagesResourceConfig.PROPERTY_PACKAGES,
                String.format("%s;%s", ThrowableMapper.class.getPackage().getName(), packageName));    

ServerProperties.PROVIDER_PACKAGES should be used for above code instead of PackagesResourceConfig.PROPERTY_PACKAGES.