簡體   English   中英

Spring boot 使用@PathVariable時報404錯誤

[英]Spring boot report 404 error when using @PathVariable

我是 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”時,我得到了“白標錯誤頁面”。

我的代碼有什么問題? 非常感謝。

試試@PathVariable("name") String name 並在 hello 方法中使用@ResponseBody注釋。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM