Which dependencies to create REST services on Tomcat7 (Netbeans project with Maven) - java

My webapp includes REST web services and is running on GlassFish 3.1.2.
I would like to run this app on Tomcat 7 instead of GlassFish. What dependencies should I add and remove to enable REST services on Tomcat?
(At the moment I just changed "GlassFish" to "Tomcat" in the "Run" menu of Netbeans, but my http requests give a 404.)
Note: this is a Maven project on Netbeans.

Ok here is the list of dependencies you may need. Please note I just put latest version but you may want to use a different version. Please also check for compatibility of these versions with each other.
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.8</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
Needed if you are using jackson to parse json
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.3.3</version>
</dependency>
This is what I am using on my tomcat but little bit older versions
Now about your 404
First check if the dependencies fix your problem. 404 may be an issue from some thing not configured right in your web.xml as well.
I hope it helps you solve your problem :)

Related

Minimal dependency on RestEasy when using Keycloak Java admin api

Keycloak Java Admin api works nice, however, including RestEasy dependencies in our pom.xml causing issues like this:
RESTEASY002307: Failed to execute
javax.ws.rs.NotFoundException: RESTEASY003210: Could not find resource for full path: http://localhost:8080/
I wonder is there any configuration for RestEasy to work with "minimal" settings which are required by Keycloak Java Admin api? Nothing more than that.
What we have tried so far without success:
Disabling rest easy scanning in web.xml
Map RestEasy to dummy url like /restdummy
Our pom.xml:
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-admin-client</artifactId>
<version>4.6.0.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.6.2.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>3.6.2.Final</version>
</dependency>
Any advice is welcome.
You can find the dependencies from mvnrepository for keycloak-admin-client
It seems that downgrading to earlier versions like 3.1.0. solved mentioned issue. However, it would be nice not to depend on whole framework just to use client functionalities.

Getting NoSuchMethodError:javax.servlet.ServletContext.getVirtualServerName()

I am facing an issue during deployment of a service in Tomcat 8. Getting following error :
Caused by: java.lang.NoSuchMethodError:
javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;
at org.apache.tomcat.websocket.server.WsServerContainer.(WsServerContainer.java:149)
at org.apache.tomcat.websocket.server.WsSci.init(WsSci.java:131)
at org.apache.tomcat.websocket.server.WsSci.onStartup(WsSci.java:47)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5244)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 10 more
Method getVirtualServerName was introduced in Servlet 3.1 and after extracting MANIFEST.MF from my servlet-api jar I got following details :
Specification-Title: Java API for Servlets
Specification-Version: 3.1
Specification-Vendor: Sun Microsystems, Inc.
Implementation-Title: javax.servlet
Which says that its having 3.1. So is there any other reason for this error? Please help
Check all your Maven (or equivalent) dependencies and make sure that you - or most likely another dependency - are not pulling in a pre-3.1 version of the javax.servlet / servlet-api that may be taking precedence over what's in your Tomcat 8. If you've manually deployed, make sure you haven't manually copied any servlet-api JARs into Tomcat itself.
See: https://stackoverflow.com/a/26232535/954442
The method getVirtualServerName has been added in ServletContext in Servlet 3.1. See the java doc's method getVirtualServerName.
This problem has 3 primary causes:
Your servlet version is older than 3.1.
Some other jar has the servlet with a version older than 3.1.
Your tomcat version is older than 8
to solve it, you can try the below way.
I. Check your pom.xml for the code below.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
if your pom.xml has the above code, it would still has that problem. you can do the second way.
II. to check your other jar has refer to the javax.servlet-api jar. for example, the org.apache.santuario has refer to the javax.servlet-api jar. the pom.xml:
<dependency>
<groupId>org.apache.santuario</groupId>
<artifactId>xmlsec</artifactId>
<version>1.4.3</version>
</dependency>
but when you look at the maven dependencies, it refer to the javax.servlet-api jar whose version is 2.3 older than 3.1.
so you should exclude the 2.3 version. pom.xml:
<!-- exclude servlet-api 2.3 jar-->
<dependency>
<groupId>org.apache.santuario</groupId>
<artifactId>xmlsec</artifactId>
<version>1.4.3</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- servlet-api 3.1 version has getVirtualServerName() -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
III. spring boot run the default tomcat 7. so define your tomcat version 8 instead of tomcat 7. so add the code your pom.xml:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<tomcat.version>8.5.5</tomcat.version>
</properties>
I had this error on IntelliJ with maven after updating IntelliJ.
I could run the tests with maven but not from my IDE.
I solved the problem by removing the ./idea and project.iml files and reloading the project.
If you have used this dependency:
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.23.0</version>
</dependency>
Then please exclude as below:
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.23.0</version>
<exclusions>
<exclusion>
<artifactId>servlet-api</artifactId>
<groupId>org.mortbay.jetty</groupId>
</exclusion>
</exclusions>
</dependency>
Spring boot will run tomcat 7 per default, you have to override maven build tomcat.version in your pom.xml. See below to run tomcat 8.0.30
<properties>
<tomcat.version>8.0.30</tomcat.version>
</properties>
Should fix your problem.
Solved
On my mac with java 8 was facing issue with downloaded tomcat from site and unzip.
My issue got solved because there was a extra servlet-api.jar file which was getting picked up. It was coming from
/Library/Java/Extensions/servlet-api.jar
For finding it in your system you can use
sudo find / -name servlet-api.jar
Removed it by backing it up somewhere else.
I was following this for intallation
https://gist.github.com/ddanailov-nmdp/c97aba2ca926b9627f6b4f7174083a32
Assuming this problem appears when you ran the application in Eclipse.
Use Dependency Hierarchy view to search for servlet-api in pom.xm
After a huge pain & sifting through all these stackoverflow answers the only thing that ended up working for me was downgrading from tomcat8 to tomcat7. I know this isn't an ideal solution, and perhaps it was just a fresh install of tomcat that solved my problem. If all else fails give that a shot.
I attach gradle style dependencies code.
dependencies {
compileOnly("javax.servlet:javax.servlet-api:3.1.0")
This surely has something to do with the version of javax.servlet and version of Tomcat.
In my case, it went away when I declared javax.servlet dependency in gradle with no version. Like this -
compile('javax.servlet:servlet-api')

adding spring resources to eclipse

I'm struggling with adding spring framework libaries to my eclipse (with maven plug-in m2eclipse)
How can I achieve it in easiest way and why is it so complicated for a newbe user?
It's really frustrating that I can't move on with thing that simple like this.
Main goal is add spring libaries to my pom.xml file in depedencies tab in my dynamic web project in eclipse. Pom.xml is generated thanks to maven plug-in.
First of all I moved to the Eclipse Marketplace and installed Spring Tool Suite for Eclipse Kepler 4.3 and the result is nothing - still can't add libaries. Second attempt was installing the same suite for my whole windows, nothing worked so far.
Sample screenshot (all I can add is this):
Where is spring-web, spring-context, spring-webmvc etc. ? For me it's night and I don't have fresh eye on it but what am i missing here?
I have been using the Spring Framework and Java within Eclipse for a while now. And to be completely honest, the UI for pom.xml completely sucks. Just avoid the Eclipse UI for Maven and manipulate the raw XML. It is very intuitive and powerful.
So if you want to add a dependency, start using mvnrepository. From there you can get all the dependency snippets you need.
For 'spring-web' insert
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
In the <dependencies></dependencies> section. And you are set.
The same can be done for context and webmvc.
Just for convenience here is spring-webmvc:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
and here is spring-context
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>

Maven dependencies for REST API - Jersey - Glassfish or not?

I'm new to REST API and little familiar with Maven. I noticed that most people use REST with Jersey implementation. I need to write POST requests and might also need GET requests as well. I have difficulty finding out the right - minimum required - dependencies for Maven to start coding.
I looked at many resources online. Looks like Jersey comes with Glassfish Server. I have Tomcat installed on my local PC which I'm going to use for development of REST and testing. We have Jboss running on our production servers which my code eventually will be deployed to. It looks to me that different resources say different things or there are chances I'm not getting it!
1) Can I use Jersey with Tomcat or Jboss WITHOUT any dependency on Glassfish?
2) Back to the Maven dependencies;
Here I found:
http://aike.me/site/blog/20090914/restful_web-services_in_java_using_jax-rs_-_part_1_getting_up_and_running
Maven: Getting Jersey into your project
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
</repository>
</repositories>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.0.3.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.1.12</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
Are these would be ALL I need?
B/c in Jersey website I found different dependencies defined:
https://jersey.java.net/documentation/latest/modules-and-dependencies.html
I won't copy and paste those dependencies here b/c it makes this post very long. I do NOT see any of these dependencies - mentioned in http://aike.me defined in:
https://jersey.java.net
Such as:
<artifactId>jersey-server</artifactId>
<artifactId>jaxb-impl</artifactId>
<artifactId>jsr311-api</artifactId>
<artifactId>jaxb-api</artifactId>
Why is that? Why I'm being referred to different dependencies?
Any help is greatly appreciated.
Using a dependency with "org.glassfish.*" in group id doesn't mean you have to use Glassfish server.
Jersey has become a part of glassfish and that's the only reason the package name has changed. The newest versions are released under this package and it is recommended on the Jersey website so I would stick with the newest packages.
I have created a quick start template for the same, feel free to fork or clone it.
Get it here

Java EE Provided dependencies in Hudson / Jenkins

I'm trying to build a Maven based Java EE project on Jenkins, but I'm getting compilation errors. The reason seems to be that the Java EE dependencies that are marked as provided in the POM logically enough aren't downloaded when the project is built.
How can I set up the POM so that the build works in Jenkins, but the EE dependencies aren't included in the WAR file?
My thanks in advance for any input you can provide.
That's strange, AFAIK the dependencies with scope "provided" are simply not placed in the built file, they should however be downloaded. Are you sure your Maven is correctly configured to download dependencies - maybe there's a proxy that's not configured.
Not sure if its the best solution, but you can add EE dependencies with scope "provided", like the example:
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>7.0.27</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
Maybe there is a plugin who provides all of them to you, but I'm not sure about that.
Hope that helps

Categories

Resources