运行 REST with Java(JAX-RS) with Jersey on IntelliJ / Tomcat
Running REST with Java(JAX-RS) with Jersey on IntelliJ / Tomcat
我正在 OSX 上尝试在 IntelliJ 中完成以下教程:
http://www.vogella.com/tutorials/REST/article.html
虽然 运行在 Eclipse 中编译代码一切正常。但是 运行 IntelliJ 中的代码会为相同的 URI 提供 404。
该程序应该 运行 在 Apache Tomcat 服务器 8.0.20 上。
我完全按照教程中的内容进行了操作,但我不明白为什么它在 IntelliJ 中不起作用。我已经搜索了几天,找到了解决方案。
看起来部署有问题,因为 index.jsp 工作正常。
希望有人能帮助我。
程序代码:
Class 您好:
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>
项目结构:(不能post图片因为信誉低,抱歉我是新手)
- 休息
- .想法
- 库
- javax.ws.rs-api-2.0.jar
- 球衣-client.jar
- 球衣-common.jar
- 球衣容器-servlet.jar
- jersey-container-servlet-core.jar
- 球衣-server.jar
- 出来
- 来源
- com.vogella.jersey.first
- 你好
- 网络
- WEB-INF
- web.xml
- index.jsp
- rest.iml
修复了此代码的问题。
我正在 OSX 上尝试在 IntelliJ 中完成以下教程:
http://www.vogella.com/tutorials/REST/article.html
虽然 运行在 Eclipse 中编译代码一切正常。但是 运行 IntelliJ 中的代码会为相同的 URI 提供 404。
该程序应该 运行 在 Apache Tomcat 服务器 8.0.20 上。
我完全按照教程中的内容进行了操作,但我不明白为什么它在 IntelliJ 中不起作用。我已经搜索了几天,找到了解决方案。
看起来部署有问题,因为 index.jsp 工作正常。
希望有人能帮助我。
程序代码:
Class 您好:
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>
项目结构:(不能post图片因为信誉低,抱歉我是新手)
- 休息
- .想法
- 库
- javax.ws.rs-api-2.0.jar
- 球衣-client.jar
- 球衣-common.jar
- 球衣容器-servlet.jar
- jersey-container-servlet-core.jar
- 球衣-server.jar
- 出来
- 来源
- com.vogella.jersey.first
- 你好
- com.vogella.jersey.first
- 网络
- WEB-INF
- web.xml
- index.jsp
- WEB-INF
- rest.iml
修复了此代码的问题。