我已经阅读了关于不解析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:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>Example API</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.example.jersey</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Example API</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
我在IntelliJ IDEA Ultimate 2016.3中运行。在https://www.dropbox.com/s/r2gjwgzj7wbkb5k/acronymServer.zip?dl=0
处提供了压缩项目目录我得到的只是404?非常感谢任何帮助。
答案 0 :(得分:0)
您是如何访问api的?它应该是http://localhost:8080/<webapp context>/helloworld
答案 1 :(得分:0)
您共享的项目存在很多配置问题,仅举几例:
/src/main/java
)不对应,因此根本不编译Java类/src/main/webapp
)lib
目录)和通过Maven war
http://localhost:8080/helloworld
servlet @Path
我已修复主要问题,以便启动并显示Hello World
,其余的将是您的家庭任务。
您可以下载固定项目here。不要忘记在运行/调试配置中更改应用程序服务器,因为我使用不同的Tomcat版本进行了测试。