java - spring boot 使用@PathVariable 报404错误

标签 java spring spring-boot

我是 Spring Boot 的初学者。

我刚刚写了一个 Controller ,代码在这里。

package hello;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
@EnableAutoConfiguration
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello Name!";
    }

    @RequestMapping(value = "/hello/{name}", method = RequestMethod.GET)
    String hello(@PathVariable String name) {
        return "hello " + name;
    }

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

然后当我访问“localhost:8080”时,我得到了正确的页面。 但是当我访问“localhost:8080/hello/someName”时,我得到了“Whitelabel Error Page”。

我的代码有什么问题?非常感谢。

最佳答案

尝试 @PathVariable("name") 字符串名称。并在 hello 方法中使用 @ResponseBody 注释。

@ResponseBody
@RequestMapping(value = "/hello/{name}", method = RequestMethod.GET)
    String hello(@PathVariable("name") String name) {
        return "hello " + name;
    }

关于java - spring boot 使用@PathVariable 报404错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44666439/

相关文章:

Java swing 使用 HTML 标签设置字体颜色,但青色和洋红色不起作用

java - MongoDB 与 Java 中的条件不同

java - 无法解析配置 ':compileClasspath' 的所有文件

java - 从使用 Spring 的 DataSourceBuilder 创建的数据源获取到 Postgresql 数据库的连接

java swing JTabbedPane stateChangedEvent

java - 如何编写一个高效/优化的循环来同时比较多个条件?八都木纸牌游戏方法

java - 从 Java 调用时 OS X 命令行应用程序无法找到目录

java - Spring 5.1.5.RELEASE 我应该如何选择 JVM 版本

java - 当我尝试在 postman 中返回消息时出现额外的空值

spring-mvc - Spring Boot 集成测试 : @AutoConfigureMockMvc and context caching