Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:REST服务URL映射_Java_Rest_Jersey_Servlet Mapping - Fatal编程技术网

Java:REST服务URL映射

Java:REST服务URL映射,java,rest,jersey,servlet-mapping,Java,Rest,Jersey,Servlet Mapping,我正在尝试使用Java创建一个非常简单的REST web服务,但是我无法正确地获得映射 这是我的服务: package rest; @Path("/square/{num}") public class SquareNumberRest { @GET @Produces(MediaType.APPLICATION_JSON) public String squareNum(@PathParam("num") String num) { int n = I

我正在尝试使用Java创建一个非常简单的REST web服务,但是我无法正确地获得映射

这是我的服务:

package rest;

@Path("/square/{num}")
public class SquareNumberRest {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String squareNum(@PathParam("num") String num) {
        int n = Integer.parseInt(num);
        int squared = n * n;
        JSONObject jo = new JSONObject(squared);
        return jo.toString();
    }
}
My 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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>TestProject</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>Jersey REST Service</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>rest</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>
我尝试使用以下URL上的服务:


但是我得到一个404错误。

您为项目定义了URL上下文吗?项目的URL根目录可能位于localhost:8080上,这意味着您需要执行以下操作


相反。

我没有明确定义任何内容,我编辑了我的问题并添加了整个web.xml文件。这台机器坏了,你试过了吗?或者artifactId?您是否尝试过localhost:8080/TestProject/square/3