PostMapping注释导致SpringBoot中出现白标签错误页面



我是Java和react web开发的新手,我的postBody方法有问题。发送发布请求后,控制台正确打印从我的前端发布的数据,但它显示了一个白标签错误页面,在localhost:8080/api中显示(type=Not Found,status=404(,在localhost:0080/api/data中显示(type=Method Not Allowed,status=405(。下面是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代码时,白标签页面会消失,并在屏幕上显示文本。

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
}
}

任何帮助都将不胜感激。

我可以看到以下几个问题,

  1. 当您尝试通过web浏览器进行调用时,它总是以HTTPGET的形式进行调用。但是在RestController中,没有GET方法。它只有一个POST方法。尝试使用Postman或CURL来调用post方法,而不是GET。

  2. 您的URL错误。由于您为PostMapping注释添加了/n,因此它应该是(http://localhost:3000/api/n(

  3. 只有您希望fullName作为请求主体,请尝试使用PathVariable。没有必要只对1个参数使用RequestBody。示例代码如下。

    @PostMapping(value="/{fullName}", consumes = MediaType.APPLICATION_JSON_VALUE)
    public String postBody(@PathVariable String fullName) {
    System.out.println(fullName);
    return "Hello " + fullName; //returns response
    }
    

最新更新