在IntelliJ / Tomcat上使用Java(JAX-RS)和Jersey运行REST。

5
我正在尝试在OSX的IntelliJ中完成以下教程:http://www.vogella.com/tutorials/REST/article.html。在Eclipse中运行代码时一切正常,但在IntelliJ中运行相同URI的代码会返回404错误。该程序应在Apache Tomcat Server 8.0.20上运行。我已经按照教程的要求做了,但仍无法弄清原因。我已经搜索了几天,还没有找到解决方案。看起来部署出现了问题,因为index.jsp能够正常工作。希望有人能帮助我。
程序代码如下:
Hello类:
package com.vogella.jersey.first;

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

// Plain old Java Object it does not extend as class or implements 
// an interface

// The class registers its methods for the HTTP GET request using the @GET annotation. 
// Using the @Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML. 

// The browser requests per default the HTML MIME type.

//Sets the path to base URL + /hello
@Path("/hello")
public class Hello {

    // This method is called if TEXT_PLAIN is request
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayPlainTextHello() {
        return "Hello Jersey";
    }

    // This method is called if XML is request
    @GET
    @Produces(MediaType.TEXT_XML)
    public String sayXMLHello() {
        return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
    }

    // This method is called if HTML is request
    @GET
    @Produces(MediaType.TEXT_HTML)
        public String sayHtmlHello() {
            return "<html> " + "<title>" + "Hello Jersey" + "</title>"
                    + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
        }

}

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" 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>com.vogella.jersey.first</display-name>
    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <!-- Register resources and providers under com.vogella.jersey.first package. -->
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.vogella.jersey.first</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app> 

项目结构:(因为声望太低不能发布图片,很抱歉作为新人)
- rest - .idea - lib - javax.ws.rs-api-2.0.jar - jersey-client.jar - jersey-common.jar - jersey-container-servlet.jar - jersey-container-servlet-core.jar - jersey-server.jar - out - src - com.vogella.jersey.first - Hello - web - WEB-INF - web.xml - index.jsp - rest.iml

这个教程展示了比你展示的更多的jar文件。只使用那些jar文件在Eclipse中能正常工作吗?在此下载完整包,将所有jar文件添加进去,然后告诉我们如果还是不能正常工作。 - Paul Samsotha
添加这些JAR文件没有改变任何东西。还是不起作用。在Eclipse中,我只添加了您刚刚发布的链接中的JAR文件,它可以工作。我还尝试将Eclipse项目(正在工作)导入到Intellij中,但它无法工作。 - Philip
1个回答

1

1
我正在尝试您的代码,但仍然遇到404错误,请问正确的页面显示URL是什么?我已经尝试了http://localhost:8080/api/hello。 - z3d0
我不是100%确定,但我认为应该是localhost:8080/rest/hello,因为<url-pattern>的缘故。 - Philip

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接