【问题标题】:Problem with mapping servlet in RESTful web applicationRESTful Web 应用程序中映射 servlet 的问题
【发布时间】:2020-06-26 18:12:59
【问题描述】:

我在 IntelliJ 的 java 8 中的 Tomcat 9 上部署简单的 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 上,我得到 HTTP 状态 404 - 在浏览器和 Postman 中均未找到
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 类:

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 导入东西:

<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


当我运行 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 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 在 WEB-INF 中,我可以在 out/artifacts/Rozpro2_war_exploded/WEB-INF/services 中看到带有 HelloWorld.java 的服务。 有人知道为什么我无法从定义的 url 得到任何响应吗?

【问题讨论】:

    标签: java rest maven tomcat intellij-idea


    【解决方案1】:

    我正在部署分解的工件,但不是在调试的情况下运行它。显然 Exploded 只能在 debug 中工作,并且存档的作品在有和没有 debug 的情况下都可以工作。

    【讨论】:

      猜你喜欢
      • 2018-01-17
      • 2012-04-17
      • 2011-04-20
      • 2016-09-05
      • 1970-01-01
      • 2013-06-13
      • 1970-01-01
      • 2023-03-08
      • 2011-09-21
      相关资源
      最近更新 更多