【问题标题】:Why Spring documentation is not perfect [closed]为什么 Spring 文档并不完美 [关闭]
【发布时间】:2017-12-28 15:33:50
【问题描述】:

嘿,我正在尝试使用 maven 构建简单的 hello world spring web 应用程序。

当我转到 url http://localhost:8080/mvc/greeting 它给我 404。

这是我的pom

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.ilyas.learning.mvcspring</groupId>
<artifactId>mvc</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>

<name>mvc</name>

<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
        <version>1.5.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.9.RELEASE</version>
        <type>jar</type>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
            <version>3.1.0</version>
        </plugin>
    </plugins>
</build>

这是问候课

@控制器 公共类 GreetingController {

@RequestMapping(value = "/greeting", method=RequestMethod.GET)
public String greeting(@RequestParam(value = "name", defaultValue = "World") String name){
    return "Hello " + name;
}

}

这里是主类。

公共类 Main 扩展 SpringBootServletInitializer{

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Main.class);
}

public static void main(String[] args) {
    SpringApplication.run(Main.class, args);
}

}

【问题讨论】:

  • 你试过没有/mvc吗?您的代码中没有任何内容指定 Spring 将部署到 /mvc 上下文。
  • 是的,我试过了,localhost:8080/mvc 工作正常并打印 index.html 上下文。
  • 我猜你希望消息Hello World!" 被返回,但它没有。当然不会,因为它是 @Controller,然后当返回 String 时,它假定它是要渲染的视图的名称。它将解析为某些模板或静态文件(因此无法找到 404)。将@ResponseBody 添加到您的方法中,表明您希望将String 作为响应正文返回,或者将@Controller 替换为@RestController

标签: java spring-mvc


【解决方案1】:

你没有发送 RequestParam 值,你需要标记它required=false 所以你的方法会

public String greeting(@RequestParam(value = "name", required=false, defaultValue = "World") String name){

【讨论】:

    【解决方案2】:

    您需要在公共类GreetingController 之前添加@RequestMapping 注释。您可以参考下面的示例代码:

    @Controller 
    @RequestMapping("/mvc")
    public class GreetingController {
    
        @RequestMapping(value = "/greeting", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
        public String greeting(@RequestParam(value = "name", defaultValue = "World") String name){
    
     //do things
    
        }
    
    }
    

    如果您创建了休息服务,那么您可以尝试在@Controller 使用@RestController

    【讨论】:

    • 它对我不起作用仍然是同样的问题。
    • 检查您的web.xmlApplicationContext.xml 文件是否位于正确的位置。并从web.xml 加载ApplicationContext.xml。使用org.springframework.web.servlet.DispatcherServlet 加载ApplicationContext
    • 我没有使用 web.xml 感染我正在使用 spring 注释进行配置
    猜你喜欢
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    • 2010-09-06
    • 2012-08-02
    • 1970-01-01
    • 2015-08-09
    • 2011-03-11
    相关资源
    最近更新 更多