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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29183513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:49:53  来源:igfitidea点击:

Running REST with Java(JAX-RS) with Jersey on IntelliJ / Tomcat

javaresttomcatwebintellij-idea

提问by Philip

I'm trying to do the following tutorial in IntelliJ on OSX:

我正在尝试在 OSX 上的 IntelliJ 中执行以下教程:

http://www.vogella.com/tutorials/REST/article.html

http://www.vogella.com/tutorials/REST/article.html

While running the code in Eclipse everything works fine. But running the code in IntelliJ delivers 404s for the same URI.

在 Eclipse 中运行代码时一切正常。但是在 IntelliJ 中运行代码会为相同的 URI 提供 404。

The program should run on a Apache Tomcat Server 8.0.20.

该程序应该在 Apache Tomcat Server 8.0.20 上运行。

I've done exactly what was written in the Tutorial but i can't figure out why it won't work in IntelliJ. I've searched for days now, finding a solution.

我已经完全完成了教程中写的内容,但我不明白为什么它在 IntelliJ 中不起作用。我已经搜索了几天,找到了解决方案。

It looks like something with the deployment is wrong, because the index.jsp is working fine.

看起来部署有问题,因为 index.jsp 工作正常。

Hope somebody can help me.

希望有人可以帮助我。

Code of the program:

程序代码:

Class 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

<?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> 

Project structure: (Can't post pictures because of low reputation, Sorry I'm new)

项目结构:(声望低不能发图,抱歉我是新手)

  • 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
  • 休息
    • 。主意
      • javax.ws.rs-api-2.0.jar
      • 球衣客户端.jar
      • jersey-common.jar
      • 球衣容器servlet.jar
      • jersey-container-servlet-core.jar
      • 球衣-server.jar
    • 出去
    • 源文件
      • com.vogella.jersey.first
        • 你好
    • 网络
      • 网络信息
        • 网页.xml
      • 索引.jsp
  • 休息.iml

采纳答案by Philip

Fixed the problem with this code.

修复了此代码的问题。

github.com/silvae86

github.com/silvae86