HTTP 状态 404 – 在 rest api 响应中未找到错误

HTTP Status 404 – Not Found error in restapi response

昨天我创造了我的第一个休息api。

这是我的代码

package RestClient;

//import javax.servlet.annotation.WebServlet;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/RestClient")
public class Restwebclient {

    @GET
    @Produces(MediaType.TEXT_XML)
    public String sayXMLHello() {
        return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
    }

}

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" version="3.1">
  <display-name>Webservice</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

<servlet>
<servlet-name>JAVA WS</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

<init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>RestClient</param-value>
</init-param>   
<load-on-startup>1</load-on-startup>
</servlet>


<servlet-mapping>
    <servlet-name>JAVA WS</servlet-name>
    <url-pattern>/Test</url-pattern>
</servlet-mapping>  

</web-app>

当我 运行 时,Apache 服务器在 eclipse 中正常启动。但是当我在本地主机上打开 url 时,我希望我的数据不会出现。它显示错误,如下面的屏幕截图所示。

https://prnt.sc/gjatoq

这些 url 也有同样的错误

http://localhost:8080/Webservice/RestClient
http://localhost:8080/Webservice/Restwebclient
http://localhost:8080/Webservice/Restwebclient/Test
http://localhost:8080/Webservice/Test

我已经尝试了每一件事,也切换了位置并选择了 "use Tomcat Installation" 选项。我真的有点卡住了。

除了本地主机 url.

上的输出数据外,一切似乎都 运行 正常

有没有人可以与我分享任何进一步让它发挥作用的东西?

当你写 @Produces(MediaType.TEXT_XML) 时,这意味着响应将包含一个名为 Content-Type 的 HTTP header,它的值将是 text/xml

但是,来自客户端的请求必须能够接受这个响应。客户端必须发送具有相同值(也可以是 application/xml)的 Accept HTTP header 否则 Web 服务器可能决定不发送响应或客户端可能决定抛出响应离开。

Chrome(或任何其他浏览器)可能不是测试剩余 api 调用(或通常的 api)的最佳工具。不仅仅是因为HTTPheaders,还因为能够使用GET以外的HTTP方法。您可以在 chrome 网上商店中搜索允许修改 http headers 的模组,或者使用合适的 http 客户端工具

我用Fiddler。它具有丰富的功能,包括对 HTTP 请求内容的完全控制。