在RESTful Web应用程序中映射Servlet存在问题。

huangapple go评论115阅读模式
英文:

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:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <servlet>
  7. <servlet-name>Jersey REST Service</servlet-name>
  8. <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
  9. <init-param>
  10. <param-name>jersey.config.server.provider.packages</param-name>
  11. <param-value>services</param-value>
  12. </init-param>
  13. <load-on-startup>1</load-on-startup>
  14. </servlet>
  15. <servlet-mapping>
  16. <servlet-name>Jersey REST Service</servlet-name>
  17. <url-pattern>/rest/*</url-pattern>
  18. </servlet-mapping>
  19. </web-app>

HelloWorld类:

  1. package services;
  2. import javax.ws.rs.GET;
  3. import javax.ws.rs.POST;
  4. import javax.ws.rs.Path;
  5. import javax.ws.rs.Produces;
  6. import javax.ws.rs.core.MediaType;
  7. @Path("hello")
  8. public class HelloWorld {
  9. @GET
  10. @Produces(MediaType.TEXT_PLAIN)
  11. public String sayHello(){
  12. return "Hello, I am text!";
  13. }
  14. @POST
  15. @Produces(MediaType.TEXT_XML)
  16. public String sayXMLHello() {
  17. return "<?xml version=\"1.0\"?>" + "<hello> Hello, I am xml!" + "</hello>";
  18. }
  19. @GET
  20. @Produces(MediaType.TEXT_HTML)
  21. public String sayHtmlHello() {
  22. return "<html> " + "<title>Hello Jersey</title>"
  23. + "<body><h1>" + "Hello, I am html!" + "</body></h1>" + "</html> ";
  24. }
  25. }

Index.jsp:

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <html>
  3. <head>
  4. <title>Title</title>
  5. </head>
  6. <body>
  7. Hey
  8. </body>
  9. </html>

pom.xml(使用Maven导入的内容):

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>services</groupId>
  7. <artifactId>Rozpro2</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <properties>
  10. <maven.compiler.source>1.8</maven.compiler.source>
  11. <maven.compiler.target>1.8</maven.compiler.target>
  12. </properties>
  13. <dependencies>
  14. <dependency>
  15. <groupId>org.glassfish.jersey.containers</groupId>
  16. <artifactId>jersey-container-servlet</artifactId>
  17. <version>2.29</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.glassfish.jersey.inject</groupId>
  21. <artifactId>jersey-hk2</artifactId>
  22. <version>2.29</version>
  23. </dependency>
  24. </dependencies>
  25. </project>

运行配置:
运行配置图片

当我运行Tomcat服务器时,我收到以下日志:

  1. [2020-03-15 11:55:24,049] Artifact Rozpro2:war exploded: Artifact is being deployed, please wait...
  2. 15-Mar-2020 11:55:25.539 INFO [RMI TCP Connection(2)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars 至少扫描了一个包含TLDJAR,但其中不包含TLD。启用此记录器的调试日志记录,以获取完整的已扫描JAR列表,但在其中未找到TLD。在扫描期间跳过不需要的JAR可以提高启动时间和JSP编译时间。
  3. [2020-03-15 11:55:27,512] Artifact Rozpro2:war exploded: Artifact is deployed successfully
  4. [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:

  1. &lt;web-app xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot;
  2. xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  3. xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd&quot;
  4. version=&quot;4.0&quot;&gt;
  5. &lt;servlet&gt;
  6. &lt;servlet-name&gt;Jersey REST Service&lt;/servlet-name&gt;
  7. &lt;servlet-class&gt;org.glassfish.jersey.servlet.ServletContainer&lt;/servlet-class&gt;
  8. &lt;init-param&gt;
  9. &lt;param-name&gt;jersey.config.server.provider.packages&lt;/param-name&gt;
  10. &lt;param-value&gt;services&lt;/param-value&gt;
  11. &lt;/init-param&gt;
  12. &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  13. &lt;/servlet&gt;
  14. &lt;servlet-mapping&gt;
  15. &lt;servlet-name&gt;Jersey REST Service&lt;/servlet-name&gt;
  16. &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
  17. &lt;/servlet-mapping&gt;
  18. &lt;/web-app&gt;

HelloWrold class:<br>

  1. package services;
  2. import javax.ws.rs.GET;
  3. import javax.ws.rs.POST;
  4. import javax.ws.rs.Path;
  5. import javax.ws.rs.Produces;
  6. import javax.ws.rs.core.MediaType;
  7. @Path(&quot;hello&quot;)
  8. public class HelloWorld {
  9. @GET
  10. @Produces(MediaType.TEXT_PLAIN)
  11. public String sayHello(){
  12. return &quot;Hello,I am text!&quot;;
  13. }
  14. @POST
  15. @Produces(MediaType.TEXT_XML)
  16. public String sayXMLHello() {
  17. return &quot;&lt;?xml version=\&quot;1.0\&quot;?&gt;&quot; + &quot;&lt;hello&gt; Hello,I am xml!&quot; + &quot;&lt;/hello&gt;&quot;;
  18. }
  19. @GET
  20. @Produces(MediaType.TEXT_HTML)
  21. public String sayHtmlHello() {
  22. return &quot;&lt;html&gt; &quot; + &quot;&lt;title&gt;&quot; + &quot;Hello Jersey&quot; + &quot;&lt;/title&gt;&quot;
  23. + &quot;&lt;body&gt;&lt;h1&gt;&quot; + &quot;Hello,I am html!&quot; + &quot;&lt;/body&gt;&lt;/h1&gt;&quot; + &quot;&lt;/html&gt; &quot;;
  24. }
  25. }

Index.jsp:<br>

  1. &lt;%@ page contentType=&quot;text/html;charset=UTF-8&quot; language=&quot;java&quot; %&gt;
  2. &lt;html&gt;
  3. &lt;head&gt;
  4. &lt;title&gt;Title&lt;/title&gt;
  5. &lt;/head&gt;
  6. &lt;body&gt;
  7. Hey
  8. &lt;/body&gt;
  9. &lt;/html&gt;

pom.xml as I import stuff with maven:<br>

  1. &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
  2. xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  3. xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  4. &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  5. &lt;groupId&gt;services&lt;/groupId&gt;
  6. &lt;artifactId&gt;Rozpro2&lt;/artifactId&gt;
  7. &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  8. &lt;properties&gt;
  9. &lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
  10. &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
  11. &lt;/properties&gt;
  12. &lt;dependencies&gt;
  13. &lt;dependency&gt;
  14. &lt;groupId&gt;org.glassfish.jersey.containers&lt;/groupId&gt;
  15. &lt;artifactId&gt;jersey-container-servlet&lt;/artifactId&gt;
  16. &lt;version&gt;2.29&lt;/version&gt;
  17. &lt;/dependency&gt;
  18. &lt;dependency&gt;
  19. &lt;groupId&gt;org.glassfish.jersey.inject&lt;/groupId&gt;
  20. &lt;artifactId&gt;jersey-hk2&lt;/artifactId&gt;
  21. &lt;version&gt;2.29&lt;/version&gt;
  22. &lt;/dependency&gt;
  23. &lt;/dependencies&gt;
  24. &lt;/project&gt;

Run configuration:
run configuration

<br>
When I run Tomcat server I get logs:<br>

  1. [2020-03-15 11:55:24,049] Artifact Rozpro2:war exploded: Artifact is being deployed, please wait...
  2. 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.
  3. [2020-03-15 11:55:27,512] Artifact Rozpro2:war exploded: Artifact is deployed successfully
  4. [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.

huangapple
  • 本文由 发表于 2020年3月15日 19:22:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/60692308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定