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

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

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:

&lt;web-app xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot;
        xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
        xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd&quot;
        version=&quot;4.0&quot;&gt;
   &lt;servlet&gt;
       &lt;servlet-name&gt;Jersey REST Service&lt;/servlet-name&gt;
       &lt;servlet-class&gt;org.glassfish.jersey.servlet.ServletContainer&lt;/servlet-class&gt;
       &lt;init-param&gt;
           &lt;param-name&gt;jersey.config.server.provider.packages&lt;/param-name&gt;
           &lt;param-value&gt;services&lt;/param-value&gt;
       &lt;/init-param&gt;
       &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
   &lt;/servlet&gt;

   &lt;servlet-mapping&gt;
       &lt;servlet-name&gt;Jersey REST Service&lt;/servlet-name&gt;
       &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
   &lt;/servlet-mapping&gt;
&lt;/web-app&gt;

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(&quot;hello&quot;)
public class HelloWorld {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayHello(){
        return &quot;Hello,I am text!&quot;;
    }


    @POST
    @Produces(MediaType.TEXT_XML)
    public String sayXMLHello() {
        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;;
    }

    @GET
    @Produces(MediaType.TEXT_HTML)
    public String sayHtmlHello() {
        return &quot;&lt;html&gt; &quot; + &quot;&lt;title&gt;&quot; + &quot;Hello Jersey&quot; + &quot;&lt;/title&gt;&quot;
                + &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;;
    }
}

Index.jsp:<br>

&lt;%@ page contentType=&quot;text/html;charset=UTF-8&quot; language=&quot;java&quot; %&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Title&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
Hey
&lt;/body&gt;
&lt;/html&gt;

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

&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;services&lt;/groupId&gt;
    &lt;artifactId&gt;Rozpro2&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
    &lt;properties&gt;
        &lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
        &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
    &lt;/properties&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.glassfish.jersey.containers&lt;/groupId&gt;
            &lt;artifactId&gt;jersey-container-servlet&lt;/artifactId&gt;
            &lt;version&gt;2.29&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.glassfish.jersey.inject&lt;/groupId&gt;
            &lt;artifactId&gt;jersey-hk2&lt;/artifactId&gt;
            &lt;version&gt;2.29&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
&lt;/project&gt;

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.

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:

确定