Migration of a Jersey based Spring rest project to Java 11 - rest

I have to migrate a rest web application to Java 11. I know, it is not trivial and I found out there many related posts. But, somehow none of them is my case.
The application is in use and runs in a Apache Tomcat 8.5.75 with Openjdk 1.8.0.222-2.b10.
Now, just simple deploying the application into an Apache Tomcat 9.0.58 with Eclipse Adoptium 11.0.14.1, the servlets are throwing java.lang.IllegalArgumentExceptions:
10-Jun-2022 08:13:45.840 SEVERE [main] org.apache.catalina.core.ApplicationContext.log Servlet.init() for servlet [RestOxServlet] threw exception
java.lang.IllegalArgumentException
at jersey.repackaged.org.objectweb.asm.ClassReader.<init>(ClassReader.java:170)
...
Such a servlet looks like this:
<servlet>
<servlet-name>RestOxServlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>
mx.ox;
mx.common.ox
</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>
mx.ox.rest.Log4jServiceLoggingFilter;
mx.common.ox.servicelogging.filter.ServiceLoggerServerFilter
</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>
my.ox.rest.Log4jServiceLoggingFilter;
mx.common.ox.servicelogging.filter.ServiceLoggerServerFilter
</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.feature.logging.DisableEntitylogging</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.feature.XmlRootElementProcessing</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
The use libraries are:
com.sun.jersey.contribs:jersey-apache-client4:jar:1.19.4:compile
com.sun.jersey.contribs:jersey-apache-client:jar:1.19.4:compile
com.sun.jersey.contribs:jersey-multipart:jar:1.19.4:compile
com.sun.jersey.contribs:jersey-spring:jar:1.19.4:test
com.sun.jersey:jersey-client:jar:1.19.4:compile
com.sun.jersey:jersey-core:jar:1.19.4:compile
com.sun.jersey:jersey-server:jar:1.19.4:compile
com.sun.jersey:jersey-servlet:jar:1.19.4:compile
It is know that with Java 11 many of those classes/packages vanished from JDK. Who has already solved this kind of problem? Is there an equivalency table with all dependencies for Java 11?
SK

Related

CSRF Guard:Owasp.CsrfGuard.js compilation error

I am trying to implement Owasp CARF library (3.0.0). When I place the Owasp.CsrfGuard.js file in WEB-INF/ folder and try to deploy the application, I get the below error:
Unexpected token % at Owasp.CsrfGuard.js
I see the %DOMAIN_STRICT% and other variables which are not recognized.
I have configured the Javscriptservlet like:
<servlet>
<servlet-name>JavaScriptServlet</servlet-name>
<servlet-class>org.owasp.csrfguard.servlet.JavaScriptServlet</servlet-class>
<init-param>
<param-name>source-file</param-name>
<param-value>/WEB-INF/Owasp.CsrfGuard.js</param-value>
</init-param>
<init-param>
<param-name>inject-into-forms</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>inject-into-attributes</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>domain-strict</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>referer-pattern</param-name>
<param-value>.*</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>JavaScriptServlet</servlet-name>
<url-pattern>/JavaScriptServlet</url-pattern>
</servlet-mapping>
The server used is Weblogic 12.2.1.2
Do I need to configure anything. How do I resolve the js error??

WebLogic changes the url-pattern of RESTful

After deploying my war file, Weblogic changes the url-pattern, which i've defined in web.xml for my Jersey RESTful. As you can see, I want to access my service under /rest/* but weblogic changes it to /resources/*. Where can I configure it for WebLogic?
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>ch.ni.service</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
p.s it works with tomcat, as desired
I ran into a similar problem moving an application from Glassfish 3 to Weblogic 12.2.1. I finally caved in and followed the Jersey tutorial here.
I used the web.xml that was generated in the example to fix the code I was working on.
Short answer is I had to change the web.xml servlet definition to look like:
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>your.package.with.JAXRS.resources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webservices/*</url-pattern>
</servlet-mapping>
I also took the dependencies from the POM in the example and used them in my POM.
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.24</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.24</version>
</dependency>
Hope it helps.

Jersey 2.1 + JBoss 7.1 NoSuchMethodError: getProperties

I am trying to run Jersey 2.1 REST service on JBoss 7.1 AS. I am getting the NoSuchMethodError: javax.ws.rs.core.Application.getProperties error during deployment:
ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/RESTService]] (MSC service thread 1-9) StandardWrapper.Throwable: java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:271) [jersey-server-2.1.jar:]
at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:283) [jersey-container-servlet-core-2.1.jar:]
In pom.xml I have:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.1</version>
</dependency>
And in the web.xml:
<servlet>
<servlet-name>RESTService</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.gatekeeper.restservice.RESTApplication</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Previously I tried with Jersey 1.17.1 and it worked (after disabling resteasy scan and jaxrs extension/subsystem in JBoss). So far I've found one similar post (but with Tomcat) where the conslusion was that the wrong javax.ws.rs.core.Application is being bound at runtime, and further that the bound class is an "old" (JAX-RS 1.1) version.
Any help how to resolve this? I'm .net guy and I'm totally blind in java :)
Thanks
Bartek
I know this thread is not that fresh, but still, I only hit this problem yesterday and nothing seems to work.
My scenario is very similar: Jersey 2.23.1 REST App (JAX-RS 2.x) already running on tomcat and need to run on JBoss 7.1.1 (built-in JAX-RS 1.1).
Turn off Restease package scanning in your web.xml:
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.providers</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.resources</param-name>
<param-value>false</param-value>
</context-param>
Remove all tags with "jaxrs" from standalone.xml. Otherwise you will still face LinkageError because JBoss keeps 1.1 spec on.
Create yourApp.war!WEB-INF\jboss-deployment-structure.xml just like is pointed out here: https://docs.jboss.org/author/display/AS7/Class+Loading+in+AS7#ClassLoadinginAS7-JBossDeploymentStructureFile
This way, not only:
java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;
Disapears, but also, JAXB works fine (No ClassNotFoundException for javax.xml.bind.JAXBException once module javax.xml.bind.api is also activated).
Obs1: The original question is mixing jersey 1.x with jersey 2.x. There is no PojoMappingFeature in jersey 2.x and base package is org.glassfish.jersey. Take a look at https://jersey.java.net/documentation/latest/migration.html#mig-1-x-json
Obs2: I have also tried other approaches like extending ResourceConfig and scanning packages from there or registering classes directly. Nothing worked like the proper documentation in item 3. So kept my servlet untouched:
<servlet>
<servlet-name>WebNize REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>br.com.webnize.rest.service</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>WebNize REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
I hope it helps others!
In short, significant structural changes have been made between 1.17 and 2.2 of the Jersey implementation. You need to:
1) Extend your application class from "org.glassfish.jersey.server.ResourceConfig"
2) Add the packages line within the application class constructor, like so:
public class MyApplication extends ResourceConfig {
public MyApplication() {
packages("com.mysite.MyApplication");
}
}
3) Within web.xml, you'll need to update a number of places. See below:
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.mysite.MyApplication</param-value>
</init-param>
4) Deploy and have fun.
Additionally, it seems that the practice of using wildcards within the java source is deprecated.
Not this)
#Path("/v1/status/*")
But this)
#Path("/v1/status")
An improvement, IMHO.

CORS Filter is not changing my header

I have a maven project (with web and JPA facets) in Eclipse Java EE IDE for Web Developers. I have added the jars to my tomcat library and everything compiles great.
However, my webpage header does NOT reflect what I have enabled in my web.xml through the CORS filter.
My header looks like:
And my web.xml contains:
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- Servlet 1: Expose the OData service endpoint -->
<servlet>
<servlet-name>OData</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<!-- all additional parameters will be passed to your factory create method -->
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>org.odata4j.jersey.producer.resources.ODataApplication</param-value>
</init-param>
<init-param>
<param-name>odata4j.producerfactory</param-name>
<param-value>org.odata4j.producer.jpa.JPAProducerFactory</param-value>
</init-param>
<init-param>
<param-name>odata4j.jpa.persistenceUnitName</param-name>
<param-value>Resource</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>OData</servlet-name>
<url-pattern>/example.svc/*</url-pattern>
</servlet-mapping>
<!-- Servlet 2: Enable crossdomain access for browser clients -->
<servlet>
<servlet-name>CrossDomain</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>org.odata4j.producer.resources.RootApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CrossDomain</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<!-- Use CORSFilter for CORS -->
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
<!-- Note: All parameters are options, if ommitted CORS Filter
will fall back to the respective default values.
-->
<init-param>
<param-name>cors.allowGenericHttpRequests</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.allowOrigin</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.supportedMethods</param-name>
<param-value>GET, HEAD, POST, OPTIONS, PUT, DELETE</param-value>
</init-param>
<init-param>
<param-name>cors.supportedHeaders</param-name>
<param-value>Content-Type, X-Requested-With, Accept, Authentication</param-value>
</init-param>
<init-param>
<param-name>cors.exposedHeaders</param-name>
<param-value>X-Test-1, X-Test-2</param-value>
</init-param>
<init-param>
<param-name>cors.supportsCredentials</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.maxAge</param-name>
<param-value>3600</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
I fixed my headers to support CORS by implementing Padcom's CORS filter for Java application as described here.
I'm still not sure why the method I tried above didn't work...
But if you are stuck trying the method above, it is easy and work trying Padcom's method. :)

OSGI gogo console in JBoss?

Fyi, I am using Maven and Eclipse 3.6
Is it possible to deploy "felix" to JBoss (4.2.3) - such that I could access the Gogo shell from my Eclipse console?
If so - what kind of "descriptor" should I use? I've only ever encountered the "servlet" type deployments, with web.xml file:
<servlet>
<servlet-name>JerseyTest</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.resources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>