简体   繁体   English

使用Javax和Jersey Servlet调度程序的Java RESTful服务入门

[英]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. 我是Web服务的新手,正在尝试配置简单的REST服务。 I am following the tutorial http://www.vogella.com/tutorials/REST/article.html 我正在关注教程http://www.vogella.com/tutorials/REST/article.html

My TestClass is 我的TestClass是

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 和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_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 我到达网址时得到404

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. 我可以理解,Jersey以某种方式提供了自己的Servlet实现,因此我不需要扩展HttpServlet。 But this class should be regietered as the tutorial says <param-value> says the package where services are there 但是应该重用此类,因为本教程说<param-value>说那里的服务包

EDIT 编辑

The application is deployed, i verified it by adding a servlet class to the same package 部署了应用程序,我通过将Servlet类添加到同一包中来对其进行了验证

@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, 现在,点击URL,

localhost:8080/ServletTest/TestServlet

is giving me the expected result 给了我预期的结果

Thanks 谢谢

Initially i was using a jar downloaded from 最初我使用的是从下载的jar

Java2s link for downloading the jar Java2s链接,用于下载jar

I replaced it with jersey bundle 1.17 and asm 3.3.1 . 我用jersey bundle 1.17asm 3.3.1代替了它。 It worked then 当时有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 https 的 Java Jersey RESTful Web 服务 - Java Jersey RESTful web service using https 如何使用Servlet创建RESTful Web服务(没有Jersey等)? - How to create a RESTful Web Service using Servlet (without Jersey, etc)? HTTP 状态 500 javax.servlet.ServletException:servlet [Jersey RESTful App] 的 Servlet.init() 抛出异常 java.lang.IllegalArgumentException - HTTP Status 500 javax.servlet.ServletException: Servlet.init() for servlet [Jersey RESTful App] threw exception java.lang.IllegalArgumentException Jersey Web服务提供了javax.servlet.ServletException: - Jersey web service gives javax.servlet.ServletException: Java:部署没有平针织的restful Web服务 - Java : Deploy restful web service without jersey 在Jersey 2中使用Hystrix Java Servlet和Servlet过滤器 - Using a Hystrix Java Servlet & Servlet Filter in Jersey 2 Swagger 与 Jersey 2 抛出 java.lang.NoClassDefFoundError: javax/servlet/ServletConfig - Swagger with Jersey 2 throws java.lang.NoClassDefFoundError: javax/servlet/ServletConfig 泽西Servlet-java.lang.NoClassDefFoundError:javax / xml / bind / JAXBException - Jersey Servlet - java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException 关于使用Jersey和Guice进行RESTful服务的实用建议 - Practical advice on using Jersey and Guice for RESTful service 使用 jQuery 访问基于 Jersey 的 RESTful 服务 - Access Jersey based RESTful service using jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM