Issue
I am new to creating API's. I found what I thought was a simple example online but was unable to get it to work. Any help or advice would be great.
This is my web.xml file
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Hello, World Application</display-name>
<description>
This is a simple web application with a source code organization
based on the recommendations of the Application Developer's Guide.
</description>
<servlet>
<servlet-name>GetEDPInfo</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>mypackage</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>GetEDPInfo</servlet-name>
<url-pattern>/GetEDPInfo</url-pattern>
</servlet-mapping>
This is my java example GetEDPInfo.java
package mypackage;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("/GetEDPInfo")
public class GetEDPInfo {
@GET
@Path("/{param}")
public Response getMsg(@PathParam("param") String msg) {
String output = "Welcome"+ msg;
return Response.status(200).entity(output).build();
}
}
I downloaded these jar files and placed them in my WEB-INF/lib directory asm-3.1.jar, jersey-core-1.8.jar, jersey-server-1.8.jar not sure if I need more than that, but those were all the ones used on the example i looked at.
I compiled my java file with the following command: javac -source 1.7 -target 1.7 -bootclasspath /usr/java/jdk1.7.0_80/jre/lib/rt.jar cp .:/var/lib/tomcat6/webapps/sample/WEB-INF/lib/jersey-core-1.8:/var/lib/tomcat6/webapps/sample/WEB-INF/lib/jersey-server-1.8:/var/lib/tomcat6/webapps/sample/WEB-INF/lib/asm-3.1 GetEDPInfo.java
That created my class file with no errors.
When i navigate to http://grv4:8080/sample/GetEDPInfo i get this message...
HTTP Status 405 - Method Not Allowed
type Status report
message Method Not Allowed
description The specified HTTP method is not allowed for the requested resource (Method Not Allowed).
When I navigate to http://grv4:8080/sample/GetEDPInfo/Jeeves i get this message...
HTTP Status 404 - /sample/GetEDPInfo/Jeeves
type Status report
message /sample/GetEDPInfo/Jeeves
description The requested resource (/sample/GetEDPInfo/Jeeves) is not available.
This is my modified code...
package mypackage;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.core.MediaType;
@Path("/GetEDPInfo")
public class GetEDPInfo {
@GET
@Path("/{param}")
@Produces(MediaType.TEXT_PLAIN) @Consumes(MediaType.TEXT_PLAIN)
public String getMsg(@PathParam("param") String msg) {
String output = "Welcome"+ msg;
return output;
}
}
Solution
Change the <param-value>mypackage.GetEDPInfo</param-value>
to following and it should work <param-value>mypackage</param-value>
Answered By - Harkamwaljeet Singh
Answer Checked By - David Goodson (JavaFixing Volunteer)