我想給我的stringsArrayList添加一個名字,但是當我呼叫 put- 方法時,console.log 會顯示一個 get 方法。
當我呼叫http://localhost:8081/strings/add/Test我得到的網址時
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Oct 22 10:49:01 CEST 2021
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported
在控制臺上我可以看到這個

但是應該是put方法而不是get方法,有什么問題嗎?
import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class BasicController {
private Logger logger = LoggerFactory.getLogger(BasicController.class);
ArrayList<String> basicList = new ArrayList<String>();
@GetMapping("/strings/get")
@ResponseBody
public String getAllStrings() {
return basicList.toString();
}
@PutMapping("/strings/add/{newString}")
@ResponseBody
public String addNewString(@PathVariable String newString) {
logger.debug(newString);
basicList.add(newString);
return newString " added";
}
}
主要的
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@RequestMapping("/")
public String hello() {
return "Hello World!";
}
}
我正在使用的 Springboot 2.5.6
uj5u.com熱心網友回復:
問題是您在瀏覽器中運行呼叫,默認情況下這將觸發 get 請求而不是 put,請查看此檔案以了解 Http 方法 Http Methods 解釋
正如您在其中一條評論中指出的那樣,您將需要 Postman 或其他類似工具才能使其正常作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/331714.html
