Json Spring端点中不需要的布尔值始终返回null
我有一个spring api端点,如下所示:Json Spring端点中不需要的布尔值始终返回null,json,spring,spring-boot,spring-mvc,Json,Spring,Spring Boot,Spring Mvc,我有一个spring api端点,如下所示: public ResponseEntity<TestObj> getCounterProposals( @PathVariable Long testObjId, @RequestBody(required = false) Boolean status) 公众响应获取反提案( @路径变量Long testObjId,@RequestBody(required=false)布尔状态) 如何发送请求以使状态参数填满。我的端
public ResponseEntity<TestObj> getCounterProposals(
@PathVariable Long testObjId, @RequestBody(required = false) Boolean status)
公众响应获取反提案(
@路径变量Long testObjId,@RequestBody(required=false)布尔状态)
如何发送请求以使状态参数填满。我的端点如下所示:
public ResponseEntity<TestObj> getCounterProposals(
@PathVariable Long testObjId, @RequestBody(required = false) Boolean status)
GET/api/test/{testId}
现在它的值总是空的,并且testId被填充。我从邮递员处发出如下请求:
public ResponseEntity<TestObj> getCounterProposals(
@PathVariable Long testObjId, @RequestBody(required = false) Boolean status)
我应该将布尔值包装到某个DTO对象中吗?我有以下工作:
@GetMapping("/greeting")
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name, @RequestBody Boolean status) {
System.out.println(status);
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
在调用端点的Postman中:
我们正在url?name=David
中传递@RequestParam name
,并在正文中传递布尔值状态
而且工作正常
请注意,所选选项卡为Body,类型为JSON。(紧跟在GraphQL BETA之后),传递的值为true
。非状态-真实
作为表单数据
,当您在邮递员请求中传递它时。因为-正如其他人所提到的那样-拥有一个带有GET
请求的身体是不传统的。
如果您的要求是将@GetMapping
与状态
参数一起使用,而该参数在查询url中不应可见,那么可能也值得一看@RequestHeaders
例如:@RequestHeader(“status”)Boolean status
请记住,GET操作不应使用body,因为GET方法意味着检索任何信息。此链接可以帮助您设计端点:@dcalap这是一项要求。它不能在查询参数中:)您需要使用POST在中发送数据RequestBody@btreport我的需求明确指出它应该是一个GET请求,并且状态在查询中不能可见string@dcalap谢谢,我也认为这个要求是无效的。现在我将使用查询路径查询参数!当然可以,但这是一个查询参数,而不是主体。它对我来说也是这样,这就是我决定使用的。不,它不是一个查询参数,而是一个请求主体。检查示例中作为true
在postman的Body选项卡中传递的@RequestBody布尔状态
。