简体   繁体   中英

Getting started with Java RESTful service using javax, and Jersey Servlet dispatcher

I am new to web services and trying to configure a simple REST service. I am following the tutorial http://www.vogella.com/tutorials/REST/article.html

My TestClass is

package com.test.servlettest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class TestRestService {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayHello() {
       return "Hello Jersey";
    }
}

And web.xml is

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
     <display-name>ServletTest</display-name>
     <servlet>
        <servlet-name>TestRestService</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.test.servlettest</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>TestRestService</servlet-name>
        <url-pattern>/test/*</url-pattern>
    </servlet-mapping>
</web-app>

I am getting 404 when i am hitting URLs

http://localhost:8080/ServletTest
http://localhost:8080/ServletTest/test
http://localhost:8080/ServletTest/test/hello

What is missing here, i am not getting it.

I could understand this, Jersey provides its own Servlet implementation somehow, so i do not not need to extend HttpServlet. But this class should be regietered as the tutorial says <param-value> says the package where services are there

EDIT

The application is deployed, i verified it by adding a servlet class to the same package

@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse res) throws     ServletException, IOException {
     res.setContentType("text/html");
    PrintWriter out=res.getWriter();
    out.println("<html><body>Hello World</body></html>");

    }
 }     

And now, hitting the URL,

localhost:8080/ServletTest/TestServlet

is giving me the expected result

Thanks

Initially i was using a jar downloaded from

Java2s link for downloading the jar

I replaced it with jersey bundle 1.17 and asm 3.3.1 . It worked then

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM