在Java API中获取404

问题描述 投票:0回答:1

'我通过访问以下URL得到404:http://localhost:8080/JavaApp/rest/hello项目名称:JavaAPP球衣版本:2.29.1Java类:请帮助我解决此问题。'

Java类:

    @Path("/hello")
    public class Hello {

          @GET
          @Produces(MediaType.TEXT_XML) 
          public String sayHello() {
          String recources="<? xml version='1.0' ?>" +
          "<hello>Hi, This is hello from XML</hello>"; 
          return recources; 
          }


    /*
     * @GET
     * 
     * @Produces(MediaType.APPLICATION_JSON) public String sayHelloJson() { String
     * recources=null; return recources; }
     * 
     * 
     * @GET
     * 
     * @Produces(MediaType.TEXT_HTML) public String sayHelloHtml() { String
     * recources="<h1>Hello form HTML </h1>."; return recources; }
     */
    }

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>JavaApp</display-name>


     <servlet>
        <servlet-name>Java Test</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
             <param-name>jersey.config.server.provider.packages</param-name>
             <param-value>com.test.api</param-value>
        </init-param>   
    </servlet>


    <servlet-mapping>
        <servlet-name>Java Test</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>
java-api
1个回答
0
投票

您还需要为方法本身定义Path,

  @GET
  @Path("/say")
  @Produces(MediaType.TEXT_XML) 
  public String sayHello() {
     String recources="<? xml version='1.0' ?>" +
       "<hello>Hi, This is hello from XML</hello>"; 
     return recources; 
  }

现在尝试呼叫http://localhost:8080/JavaApp/rest/hello/say

© www.soinside.com 2019 - 2024. All rights reserved.