I was facing problems while running the restful api
Adding below import solved the problem
Tomcat version - 8.5
Java - 8
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Code :
package test;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@Path("/hello")
public class Hello {
@GET
@Produces(MediaType.TEXT_XML)
public String sayHelloXML() {
String resource = "<? xml version='1.0' ?>" + "<hello> Hi Pragya </hello>";
return resource;
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public String sayHelloJSON() {
String resource = null;
return resource;
}
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHello(@QueryParam("name") String name) {
String resource = "<h1> this is HTML method </h1>" + "Hi "+name;
return resource;
}
}
web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>JavaRestAPI</display-name>
<servlet>
<servlet-name>JAVA 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>test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAVA API</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
URL:
http://localhost:8000/JavaRestAPI/rest/hello?name=pragya
I am using port 8000 coz 8080 had some other thing running
References: https://www.youtube.com/watch?v=5jQSat1cKMo
URL:
http://localhost:8000/JavaRestAPI/rest/hello?name=pragya
I am using port 8000 coz 8080 had some other thing running
References: https://www.youtube.com/watch?v=5jQSat1cKMo
No comments:
Post a Comment