英文:
Problem with mapping servlet in RESTful web application
问题
以下是您要翻译的内容:
我在使用Java 8中的Tomcat 9在IntelliJ中部署简单的helloworld RESTful Web应用程序时遇到问题。我只能在http://localhost:8080/Rozpro2_war_exploded/
下收到index.jsp页面。
在地址../Rozpro2_war_exploded/rest | ../Rozpro2_war_exploded/rest/hello | ../rest | ../rest/hello | ../Rozpro2/hello | ../hello
上,无论在浏览器还是在Postman中,我都收到HTTP状态404 - 未找到的错误。
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_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>services</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>
HelloWorld类:
package services;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("hello")
public class HelloWorld {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayHello(){
return "Hello, I am text!";
}
@POST
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> Hello, I am xml!" + "</hello>";
}
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>Hello Jersey</title>"
+ "<body><h1>" + "Hello, I am html!" + "</body></h1>" + "</html> ";
}
}
Index.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
Hey
</body>
</html>
pom.xml(使用Maven导入的内容):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>services</groupId>
<artifactId>Rozpro2</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.29</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.29</version>
</dependency>
</dependencies>
</project>
运行配置:
运行配置图片
当我运行Tomcat服务器时,我收到以下日志:
[2020-03-15 11:55:24,049] Artifact Rozpro2:war exploded: Artifact is being deployed, please wait...
15-Mar-2020 11:55:25.539 INFO [RMI TCP Connection(2)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars 至少扫描了一个包含TLD的JAR,但其中不包含TLD。启用此记录器的调试日志记录,以获取完整的已扫描JAR列表,但在其中未找到TLD。在扫描期间跳过不需要的JAR可以提高启动时间和JSP编译时间。
[2020-03-15 11:55:27,512] Artifact Rozpro2:war exploded: Artifact is deployed successfully
[2020-03-15 11:55:27,512] Artifact Rozpro2:war exploded: Deploy took 3,463 milliseconds
web.xml位于WEB-INF目录下,我可以在out/artifacts/Rozpro2_war_exploded/WEB-INF/services中看到HelloWorld.java文件。
是否有人知道为什么我无法从定义的URL获取任何响应呢?
英文:
I have a problem with deploying simple helloworld restful web application on Tomcat 9 in java 8 in IntelliJ. Only thing I recive is index.jsp page under http://localhost:8080/Rozpro2_war_exploded/
<br>
On addresses ../Rozpro2_war_exploded/rest | ../Rozpro2_war_exploded/rest/hello | ../rest | ../rest/hello | ../Rozpro2/hello | ../hello
I get HTTP Status 404 – Not Found, both in browser and in Postman<br>
web.xml:
<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_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>services</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>
HelloWrold class:<br>
package services;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("hello")
public class HelloWorld {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayHello(){
return "Hello,I am text!";
}
@POST
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> Hello,I am xml!" + "</hello>";
}
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello Jersey" + "</title>"
+ "<body><h1>" + "Hello,I am html!" + "</body></h1>" + "</html> ";
}
}
Index.jsp:<br>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
Hey
</body>
</html>
pom.xml as I import stuff with maven:<br>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>services</groupId>
<artifactId>Rozpro2</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.29</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.29</version>
</dependency>
</dependencies>
</project>
Run configuration:
run configuration
<br>
When I run Tomcat server I get logs:<br>
[2020-03-15 11:55:24,049] Artifact Rozpro2:war exploded: Artifact is being deployed, please wait...
15-Mar-2020 11:55:25.539 INFO [RMI TCP Connection(2)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
[2020-03-15 11:55:27,512] Artifact Rozpro2:war exploded: Artifact is deployed successfully
[2020-03-15 11:55:27,512] Artifact Rozpro2:war exploded: Deploy took 3,463 milliseconds
web.xml is in WEB-INF and I can see services in out/artifacts/Rozpro2_war_exploded/WEB-INF/services with HelloWorld.java inside.
Does anybody have any idea why I'm not able to get any response from defined url?
答案1
得分: 0
我正在部署已解压的构件,并且没有使用调试模式来运行它。显然,只有在调试模式下,已解压的构件才能正常工作,而已归档的构件在有无调试模式下都可以工作。
英文:
I was deploying exploded artifact and was running it not with debug. Apparently Exploded can be works only in debug, and archived works on both with and without debug.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论