尝试运行HelloWorld REST服务时获取404

我正在关注https://medium.com/@jamsesso/starting-out-with-jersey-apache-tomcat-using-intellij-6338d93ffd40#.9rmard5sl上的示例

我已经阅读了关于不解析com.sun.jersey.api.container.httpserver.HttpServerFactory的问题并且已经过去了(谢谢!)

这是我的代码:

package com.webbo.acronymserver; import com.sun.jersey.api.container.httpserver.HttpServerFactory; import com.sun.net.httpserver.HttpServer; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import java.io.IOException; /** * Created by mark on 8/3/17. */ // The Java class will be hosted at the URI path "/helloworld" @Path("/helloworld") public class acronymServer { // The Java method will process HTTP GET requests @GET // The Java method will produce content identified by the MIME Media type "text/plain" @Produces("text/plain") public String getClichedMessage() { // Return some cliched textual content return "Hello World"; } public static void main(String[] args) throws IOException { HttpServer server = HttpServerFactory.create("http://localhost:8080/"); server.start(); } } 

这是web.xml:

    Example API com.sun.jersey.spi.container.servlet.ServletContainer  com.sun.jersey.config.property.packages com.example.jersey   com.sun.jersey.api.json.POJOMappingFeature true    Example API /*   

我正在使用IntelliJ IDEA Ultimate 2016.3。 压缩项目目录位于https://www.dropbox.com/s/r2gjwgzj7wbkb5k/acronymServer.zip?dl=0

我得到的只是404? 任何帮助非常感谢。

您共享的项目有很多配置问题,仅举几例:

  • sources位置与标准Maven布局( /src/main/java )不对应,因此根本不编译Java类
  • 网络应用程序位置也不正确(应该是/src/main/webapp
  • 手动配置依赖项( lib目录)和Maven
  • 依赖项未添加到服务器上部署的工件
  • Maven包装不会发生war
  • 重复和错误配置的Web构面
  • 启动URL设置为上下文的根,而不是http://localhost:8080/helloworld servlet @Path

我修复了主要问题,以便它启动并显示Hello World ,其余的将是你的家庭任务。

结构体

您可以在此处下载固定项目。 不要忘记在运行/调试配置中更改应用程序服务器 ,因为我使用不同的Tomcat版本进行了测试。

你是如何访问api的? 它应该是http://localhost:8080//helloworld