我是 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/