Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
614 views
in Technique[技术] by (71.8m points)

java 11 - Upgrading from jersey 1.19.4 to 2.29 for Java11

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, "/*");
question from:https://stackoverflow.com/questions/65882012/upgrading-from-jersey-1-19-4-to-2-29-for-java11

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
("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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...