请求后找不到http状态404http://localhost:4080/testapi/rest/你好

ecr0jaav  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(330)

下面是my web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>testAPI</display-name>

<servlet>
 <servlet-name>testAPI</servlet-name>
 <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
 <init-param>
  <param-name>jersey.config.server.provider.packages</param-name>
  <param-value>test</param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
</servlet>
 <servlet-mapping>
   <servlet-name>testAPI</servlet-name>
   <url-pattern>/rest/*</url-pattern>
 </servlet-mapping>
</web-app>

我的类文件如下所示:

package testAPI;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class Hello {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayHello()
    {
        String resource="<? xml version='1.0' ?>" + "<hello> hello from XML</hello>";
        return resource;
    }
    /*@GET
    @Produces(MediaType.APPLICATION_JSON)
    public String sayHelloJSON()
    {
        String resource=null;
        return resource;
    }*/

}

我的java版本是8,tomcat 7和jersey 2.25。我创建了一个动态web项目。当我进入http://localhost:4080/-tomcat主页打开。http://localhost:4080/testapi/将显示index.html。但是当我尝试访问http://localhost:4080/testapi/rest/hello-未找到http状态404

cfh9epnr

cfh9epnr1#

web.xml中的param值需要指向包含类的包。

<init-param>
  <param-name>jersey.config.server.provider.packages</param-name>
  <param-value>test</param-value>
 </init-param>

应该是

<init-param>
  <param-name>jersey.config.server.provider.packages</param-name>
  <param-value>testAPI</param-value>
</init-param>

相关问题