[英]PostMapping annotation causes a whitelabel error page in SpringBoot
我是 Java 和反應 web 開發的新手,我的 postBody 方法有問題。 發送發布請求后,控制台會正確打印從我的前端發布的數據,但它會顯示一個白標簽錯誤頁面,在 localhost:8080/api 中顯示 (type=Not Found, status=404) 和 (type=Method Not Allowed, status=405) 在 localhost:8080/api/data. 下面是 HelloController 代碼。
package com.java.java.practice;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.MediaType;
import org.springframework.web.cors.CorsConfiguration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
@RestController
@RequestMapping("/api")
public class HelloController {
//this performes a post and get request
@CrossOrigin(origins = "http://localhost:3000/api")
@PostMapping(value="/n", consumes = MediaType.APPLICATION_JSON_VALUE)
public String postBody(@RequestBody String fullName) {
System.out.println(fullName);
return "Hello " + fullName; //returns response
}
}
下面是主要應用
package com.java.java.practice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages="com.java.java.practice")
public class PracticeApplication {
public static void main(String[] args) {
SpringApplication.run(PracticeApplication.class, args);
}
}
奇怪的是,當我用下面發布的代碼替換我的 HelloController 代碼時,whitelabel 頁面消失並在屏幕上顯示文本。
package com.java.java.practice;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.MediaType;
import org.springframework.web.cors.CorsConfiguration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
@RestController
public class HelloController {
//this performes a post and get request
@CrossOrigin(origins = "http://localhost:3000/api")
@RequestMapping("/api")
public String postBody() {
return "Hello"; //returns response
}
}
任何幫助將非常感激。
我可以看到以下幾個問題,
當您嘗試通過 web 瀏覽器調用時,它總是調用 HTTP GET。 但是在您的 RestController 中,沒有 GET 方法。 它只有一個 POST 方法。 嘗試使用 Postman 或 CURL 來調用 post 方法而不是 GET。
您的 URL 是錯誤的。 由於您為 PostMapping 注釋附加/n
它應該是( http://localhost:3000/api/n
)
只有您希望 fullName 作為請求正文嘗試使用 PathVariable 。 沒有必要僅將 RequestBody 用於 1 個參數。 示例代碼如下。
@PostMapping(value="/{fullName}", consumes = MediaType.APPLICATION_JSON_VALUE) public String postBody(@PathVariable String fullName) { System.out.println(fullName); return "Hello " + fullName; //returns response }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.