當我呼叫以下 api 時,即使我們提供了帶有請求正文的有效資料,它也會給出 400 個錯誤請求。如果我洗掉 @Valid 注釋,那么它作業正常。任何人都可以在這里幫助我出了什么問題。
我正在使用 Postman { "x": "a", "y": "b" } 發送以下請求正文
import javax.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.magicsoftware.restapi.exception.TestPOJO;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
@RestController
@Validated
@RequestMapping(path = "/Test")
public class TestController {
@GetMapping(value = "/test1")
public String test1(@Valid @RequestBody TestPOJO tpj) {
if (tpj instanceof TestPOJO) {
System.out.println("Correct data format passed ");
}
return "working1";
}
}
import javax.validation.constraints.NotNull;
public class TestPOJO {
@NotNull(message="X should not be null")
private String x;
@NotNull(message="Y should not be null")
private String y;
public String getX() {
return x;
}
public void setX(String x) {
this.x = x;
}
public String getY() {
return y;
}
public void setY(String y) {
this.y = y;
}
}
uj5u.com熱心網友回復:
您正在進口RequestBody, io.swagger.v3.oas.annotations.parameters.RequestBody;
但它應該從進口
org.springframework.web.bind.annotation.RequestBody;
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/490382.html
