Simple RESTful Service Using Jersey and Eclipse

I am trying to create a simple RESTful service using Jersey and Eclipse. I copied the code from http://www.ibm.com/developerworks/web/library/wa-aj-tomcat/#ibm-pcon . When I tried to start the service, I got HTTP Status 404- / Jersey /

Service: HelloResource.java

 package sample.hello.resources; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("/hello") public class HelloResource { @GET @Produces(MediaType.TEXT_PLAIN) public String sayHello() { return "Hello Jersey"; } } 

web.xml :

 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Jersey</display-name> <servlet> <servlet-name>REST Service</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>sample.hello.resources</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>REST Service</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app> 

Please help when starting the above service successfully ... donno where I did wrong.

+4
source share
1 answer

You must use the following URL to access the resource,

 http://127.0.0.1:8080/<app_name>/rest/hello 

The following site containing a good / simple example, you can try if the above does not work. http://lasithtechavenue.blogspot.com/2012/12/implementing-rest-service-with-java.html

0
source

Source: https://habr.com/ru/post/1445947/


All Articles