我在请求正文中定义了一个简单的 API,其中包含一个名为 Foo 的 JSON 对象:
@PostMapping(value = "/foo", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Foo> foo(@RequestBody Foo request) {
log.info("request:{}", request);
log.info("request.id:{}", request.getId());
return new ResponseEntity<Foo>(request, HttpStatus.OK);
}
这是
Foo
类定义:
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter @Setter
@AllArgsConstructor
@NoArgsConstructor
public class Foo {
String id;
}
这是curl脚本:
curl --location 'localhost:8080/notification-api/foo' \
--header 'Content-Type: application/json' \
--data '{"id":"a string"}'
但是当调用curl脚本时,
requst.id
方法中的foo()
值始终为空。
谢谢@KyreX ...如果您发布答案,我会接受。
问题在于 RequestBody 来自错误的导入:
*import io.swagger.v3.oas.annotations.parameters.RequestBody;*
而不是:
*import org.springframework.web.bind.annotation.RequestBody;*