【问题标题】:Spring boot report 404 error when using @PathVariableSpring boot 使用@PathVariable时报404错误
【发布时间】:2017-06-21 03:36:41
【问题描述】:

我是 Spring Boot 的初学者。

我刚刚写了一个控制器,代码在这里。

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”。

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

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    试试@PathVariable("name") String name。并在 hello 方法中使用 @ResponseBody 注释。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-18
      • 2019-03-20
      • 2021-05-04
      • 2017-11-02
      • 2017-09-06
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多