So I'm building a small rest api with java using Jersey 2.23.2. I have run into a problem however, I am not able to specify the servlet in the web.xml file properly. This is how my files look like:
pom.xml
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.23.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.23.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
web.xml
...
<servlet>
<servlet-name>WebService</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>api-mashup-api.API</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>WebService</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
API.java
package api;
@Path("/v1")
public class API {
private Controller controller;
public API(){
controller = new Controller();
}
@GET
@Produces(MediaType.TEXT_HTML)
public String print1() {
return "Please specify what resource you need.";
}
/**
* Prints to the screen if we are in /v1/foo
*
* @return
*/
@GET
@Path("/foobar")
@Produces(MediaType.TEXT_HTML)
public String print2() {
return "Please specify what resource you need.";
}
}
My project folder structure looks like this:
api-mashup-api/Java Resources/src/api/API.java
I used Jersey 1.x before and then I migrated to 2.x and now I'm not sure what to put into the params of the servlet in web.xml. When I try to run the API.java on my tomcat server I get the following exception:
SEVERE: Servlet [WebService] in web application [/api-mashup-api] threw load() exception
java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
I can reach my index.html just fine though, so the server works, but not the API part. Not sure what to do here. Obviously something is wrong with the servlet part.
EDIT-Things I have tried:
Added API.java to the param-valye in the web.xml
Looked at Basic full configuration for Jersey on Tomcat in eclipse And implemented the solution. Now when running the api, the server won't crash, but I can't find any resource with http://localhost:8080/api-mashup-api/v1 for example.
I've had and fixed this error many times. It's basically always a dependency issue.
Here are the steps to fix it:
Make sure you're using the most recent version of all packages (ie 2.23.2 of glassfish) and servers, updating your POM accordingly. This is very important for your case since you're using the new version of Jersey.
Refresh your project
Use the Maven menu to clean and build
Use the Maven menu to refresh (this is different from the project refresh)
Don't skip steps or assume that if you've done some of them here and there in the past that it's the same as doing them one after the other.
Probably issue with servlet container "jersey-container-servlet-core" is for servlet 2.x containers, try changing to
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.23.2</version>
<scope>provided</scope>
</dependency>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.