Java 其中是为使用SpringMVC创建的RESTAPI定义的url映射

Java 其中是为使用SpringMVC创建的RESTAPI定义的url映射,java,spring,rest,maven,spring-mvc,Java,Spring,Rest,Maven,Spring Mvc,我在这里学习了教程 首先让我使用maven设置pom文件,然后让我设置对象并创建资源控制器 所以我做了,也做了 package hello; import java.util.concurrent.atomic.AtomicLong; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import o

我在这里学习了教程 首先让我使用maven设置pom文件,然后让我设置对象并创建资源控制器

所以我做了,也做了

package hello;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, name));
    }
}
教程上说

虽然可以将此服务打包为传统的WAR文件,以便部署到外部应用程序服务器,但下面演示的更简单的方法将创建一个独立的应用程序

但我使用的是战争,我使用它们而不是应用程序,所以我没有创建它

然而,当我启动WAR时,它从来没有解释如何映射/问候语。它可以工作,但转到/问候语会导致404错误。这并不让我感到惊讶,因为它从未被映射到任何地方


我是否应该以某种方式将其映射到web.xml?

是的,您需要通过web.xml文件指示应用程序服务器。 如果您使用spring,这个配置(SpringMVC模板项目提供的基本配置)将起作用

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

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
请注意元素。您告诉您的应用程序服务器您有一个名为appServlet的servlet,它将被映射到/url模式。这意味着每个带有URL的请求http://.../YourApplicationName/whatever 将被转发到您的SpringServlet,并最终转发到您的控制器,后者将管理任何部分;-

如果您具有以下项目结构,上述配置将起作用:


您需要一个web mvc配置。有吗?您没有使用Spring boot?一般来说,如果您部署到一个容器中,该容器将把您的应用程序放在/appName,因此您可以转到/appName/greeting。SpringBoot将设置一个嵌入式容器,并将应用程序放在根目录下。