I am trying to view the list of my jersey rest service methods in API Documentation using Swagger. Went through few examples/sample given in GitHub sites. But still I am not able to list out my service methods when I try to access the context-root link. Getting 404 service not found.
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.danfoss.des</groupId>
<artifactId>SampleRestProject</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SampleRestProject Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey-jaxrs</artifactId>
<version>1.5.0</version>
</dependency>
<!-- <dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jaxrs_2.9.1</artifactId>
<version>1.2.0</version>
<scope>compile</scope>
</dependency> -->
</dependencies>
<build>
<finalName>SampleRestProject</finalName>
</build>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>helloworld</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>io.swagger.jaxrs.listing,com.danfoss.des</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<!-- <init-param>
<param-name>api.version</param-name>
<param-value>1.0.0</param-value>
</init-param>
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>http://localhost:8081/SampleRestProject/</param-value>
</init-param> -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>JerseyJaxrsConfig</servlet-name>
<servlet-class>io.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
<init-param>
<param-name>api.version</param-name>
<param-value>1.0</param-value>
</init-param>
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>http://localhost:8081/SampleRestProject/rest</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>helloworld</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<!-- <welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list> -->
</web-app>
Service java class:
package com.danfoss.des;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.danfoss.model.Track;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
#Path("/helloWorld")
#Api(value="helloWorld", description="Sample hello world swagger service")
public class RESTfulHelloWorld
{
#GET
#Produces("text/html")
#Path("/startingPage")
#ApiOperation(value="Starting of the swagger service")
public Response getStartingPage()
{
String output = "Staring method is invoked";
return Response.status(200).entity(output).build();
}
}
My Project structure
The link am trying to access to view the list: http://localhost:8081/SampleRestProject/api-docs
Can someone please help me find out where exactly am going wrong or if I am missing out anything.
The JerseyJaxrsConfig class is part of the swagger-jersey2-jaxrs library and hence is not available when deploying the webapp, because you're using swagger-jersey-jaxrs combined with Jersey 1.9 (which is good).
Simply replace
<servlet-class>io.swagger.jaxrs.config.JerseyJaxrsConfig</servlet-class>
with
<servlet-class>io.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>
to use the correct configuration class and then try accessing http://localhost:8081/SampleRestProject/rest/swagger.json again.
Also while you're at it, consider defining Swagger's basepath as a relative path so you're able to deploy the webapp on different ports.
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>/SampleRestProject/rest</param-value>
</init-param>
Related
I am new to swagger and jersey.
My api operations are working fine. I followed the steps in the link: https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5 to add swagger into my api. When I give my try to access the swagge.json via the url:http://localhost:8080/messenger/webapi/swagger.json I get 404 -Not found. Here are my api codes. Can you please help me out with this.
pom.xml file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.vishwas</groupId>
<artifactId>messenger</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>messenger</name>
<build>
<finalName>messenger</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey2-jaxrs</artifactId>
<version>1.5.0</version>
</dependency>
</dependencies>
<properties>
<jersey.version>2.16</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" 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_2_5.xsd">
<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>
io.swagger.jaxrs.listing,
com.vishwas.messenger
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>Swagger Configuration</servlet-name>
<servlet-class>com.vishwas.messenger.sevlet.SwaggerConfigurationServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Jersey2Config</servlet-name>
<servlet-class>io.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
<init-param>
<param-name>api.version</param-name>
<param-value>1.0</param-value>
</init-param>
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>http://localhost:8080/messenger/webapi</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
</web-app>
3.Beans config class for swagger
package com.vishwas.messenger.sevlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import io.swagger.jaxrs.config.BeanConfig;
public class SwaggerConfigurationServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
public void init(ServletConfig config) throws ServletException {
super.init(config);
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0");
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setHost("localhost:8080");
beanConfig.setTitle("Messenger Api documentation");
beanConfig.setBasePath("/messenger/webapi");
beanConfig.setResourcePackage("com.vishwas.messenger.resources");
beanConfig.setPrettyPrint(true);
beanConfig.setScan(true);
}
}
[output screenshot][1]
[1]: https://i.stack.imgur.com/fLx83.png
I found the solution to the problem, I changed the swagger-jersey2-jaxrs artifact version to the latest one and also installed the compile dependencies along with it which was provided in the maven repository website.
Please check the version properly!!
I am trying to make a RESTFUL JAVA WEB SERVICE using eclipse and maven in Apache Tomcat. Everything seems fine to me and it is not showing any error but when open the URL it shows 404 Error.
I am trying to call it via this URL http://localhost:8080/first/rest
WEB.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>first</display-name>
<servlet>
<servlet-name>firstapp</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.first.pkg</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>firstapp</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>task</groupId>
<artifactId>first</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>first Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>maven2-repository.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.9</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
</build>
</project>
RestServ.java
package com.first.pkg;
import java.util.Date;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
#Path("/")
public class RestServ {
#GET
#Produces("text/html")
public Response getStartingPage()
{
String output = "<h1>Hello World!<h1>" +
"<p>RESTful Service is running ... <br>Ping # " + new Date().toString() + "</p<br>";
return Response.status(200).entity(output).build();
}
}
<display-name>first</display-name>
is a description part and I don't think you can call your web service via this name. Instead, try calling it like
http://localhost:8080/{nameOfYourWARFileHere}/rest
Also make sure to put rest into your #Path("/") annotation in RestServ.java right after the slash otherwise the request mapping won't work.
I have been working with the restfull webservices provide by Jersey in combination with the maven jetty plugin at my previous job. But now for a personal project i am starting from scratch by myself and i cant seem to get even the basic version going. I have tried to follow a few tutorial but they are either old or off topic.
The most recent example i tried was:
jersey 2 + spring 4 + jetty-maven-plugin
So the below configs make the plug in run but no matter which url i try it gives me 404's
pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<jetty.version>9.3.11.v20160721</jetty.version>
<jersey.version>2.23.1</jersey.version>
</properties>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
</plugin>
</plugins>
</build>
</project>
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>test</display-name>
<servlet>
<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.foo.bar</param-value>
</init-param>
<!--<load-on-startup>1</load-on-startup>-->
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
resourceConfiguration.java
package com.foo.bar;
import org.glassfish.jersey.server.ResourceConfig;
public class ResourceConfiguration extends ResourceConfig {
public ResourceConfiguration() {
register(entrypoint.class);
}
}
and my service class
package com.foo.bar;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
public class entrypoint {
#GET
#Path("/test")
#Produces(MediaType.TEXT_PLAIN)
public String test() {
return "it works";
}
#GET
#Path("/")
#Produces(MediaType.TEXT_HTML)
public String test2() {
return "it works";
}
}
Nothing fancy right i feel like this should work out of the box.
The Jetty server starts but like i said i get jetty 404 pages at every url permutation i try.
Thanks in advance for any and all input.
Few Changes need to be done to make it work. please follow the below steps.
Step 1: change your web.xml like below with the POJO mapping and Resourse configuration class instead of package name.
<servlet>
<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>bar.com.ravi.ResourceConfiguration</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>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Step 2: change your ResourceConfiguration class like below with package information.
public class ResourceConfiguration extends ResourceConfig {
public ResourceConfiguration() {
packages("bar.com.ravi");
register(Entrypoint.class);
}
}
Step 3: Change your EntryPoint class with Class level #Path Annotation.
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/hi")
public class Entrypoint {
#GET
#Path("/test")
#Produces(MediaType.TEXT_PLAIN)
public String test() {
return "it works";
}
#GET
#Path("/")
#Produces(MediaType.TEXT_HTML)
public String test2() {
return "it works";
}
}
Step 4: Change your POM.xml in build tag like below.
<build>
<finalName>bar</finalName>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.11.v20150529</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webApp>
<contextPath>/bar</contextPath>
</webApp>
<httpConnector>
<port>8888</port>
</httpConnector>
</configuration>
</plugin>
</plugins>
</build>
Step 5: run below command your command promt.
mvn jetty:run
Step 6: hit the browser with below URL
http://localhost:8888/bar/hi/test
The obvious thing that is wrong on your configuration is
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.foo.bar</param-value>
</init-param>
I believe this must be
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.foo.bar.ResourceConfiguration</param-value>
</init-param>
then add the package where your resource classes are currently sitting.
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.foo.bar</param-value>
</init-param>
Try this:
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
<configuration>
<webApp>
<contextPath>/bar</contextPath>
</webApp>
</configuration>
</plugin>
and direct your browser to http://localhost:8080/bar/test, for example.
I am trying to create a rest api using jersey on glassfish 4 server.
Currently i am getting error 404 whenever I go to my mapping:
http://localhost:8080/LibraryRestTest/api/books
Sadly i get 0 errors on my glassfish console log. I have also tried manually deploying my war on glassfish admin console to result in the same error.
Is there something I haven't yet implemented ?
All files i have created in my project :
glassfish-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>
<context-root>/LibraryRestTest</context-root>
</glassfish-web-app>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<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>
com.koen.library
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.koen</groupId>
<artifactId>LibraryRestTest</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>WebContent\WEB-INF\web.xml</webXml>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<jersey.version>2.22</jersey.version>
<parser.version>2.22</parser.version>
</properties>
<dependencies>
<dependency>
<groupId>com.koen.library</groupId>
<artifactId>libraryJPA</artifactId>
<version>0.0.1</version>
</dependency>
<!-- Jersey -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${parser.version}</version>
</dependency>
</dependencies>
</project>
BookResource.java
package com.koen.library;
import java.util.ArrayList;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import com.koen.library.pojo.Book;
#Path("books")
public class BookResource {
#GET
#Produces("application/json")
public ArrayList<Book> getAll(){
return new ArrayList<Book>(){{add(new Book(6, "The Brothers Karamazov", "Fyodor Dostoevsky"));}};
}
#GET
#Path("/{id}")
public Book getById(#PathParam ("id") int id){
return null;
}
}
Instead of mapping a jersey servlet in web.xml, you could configure your REST endpoint using Java code using standard Java EE 7 API (Application and #ApplicaionPath:
#ApplicationPath("api")
public class ApplicationConfig extends Application {
}
This will automatically attach all resources annotated with #Path to the api suffix.
If you do not want all resources to map to this suffix, you may list each class in particular, if you override getClasses() method:
#ApplicationPath("api")
public class ApplicationConfig extends Application {
#Override
public Set<Class<?>> getClasses() {
return Arrays.asList(com.koen.library.BookResource.class);
}
}
The fix to my problem was:
<param-value>true</param-value>
I had to add that line in my web.xml.
Now i don't get any 404 error on my mapping.
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<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>
com.koen.library
</param-value>
</init-param>
<param-value>true</param-value>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
This is my web.xml
<servlet>
<servlet-name>Simulator HTTP API</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Simulator HTTP API</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
and this is my simple web service:
#Path("partner")
public class PartnerAPI {
#Path("/mt")
#GET
#Produces(MediaType.TEXT_PLAIN)
public String sendMT() {
return "Sent";
}
}
when i call it like this:
http://localhost:8080/myprojectname/partner/mt
i get 404 error mot found, what am i doing wrong?
Update
this is my maven
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.22.1</version>
</dependency>
You have different deployment options in Jersey 2:
If you want to do it via web.xml you have to add the an init-param where you specify which packages should be scanned:
<servlet>
<servlet-name>Simulator HTTP API</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>insert.packagename.where.your.class.is.here</param-value>
</init-param>
</servlet>
Another option is to create a basic class to configure your REST application.
This would look like this:
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
#ApplicationPath("/test")
public class YourApplication extends ResourceConfig {
public YourApplication() {
this.packages("insert.packagename.where.your.class.is.here");
}
}
Make sure to update the string with the package name where your PartnerAPI class is.
Then add the value inside #ApplicationPath to your URL.
The link would look like this: http://localhost:8080/myprojectname/test/partner/mt
More information: Jersey docs: Chapter 4. Deploying a RESTful Web Service