Spring boot Webflux@RequestBody返回400个错误的请求
在控制器参数中开发控制器webflux on post请求时,我使用注释传递请求体,但在webtestclient方法中返回400 BAD_请求 我的DTO:Spring boot Webflux@RequestBody返回400个错误的请求,spring-boot,http-post,spring-webflux,Spring Boot,Http Post,Spring Webflux,在控制器参数中开发控制器webflux on post请求时,我使用注释传递请求体,但在webtestclient方法中返回400 BAD_请求 我的DTO: @AllArgsConstructor @Data public class VisitRequest { private String description; public static Visit createEntityFromDto(Long customerId, Long deviceId, VisitR
@AllArgsConstructor
@Data
public class VisitRequest {
private String description;
public static Visit createEntityFromDto(Long customerId, Long deviceId, VisitRequest visitRequest) {
return new Visit(null, customerId, deviceId, visitRequest.getDescription(),
null);
}
}
@RestController
@RequestMapping(value = VisitController.REST_URL)
@AllArgsConstructor
@Slf4j
public class VisitController {
static final String REST_URL = "/api/customers/{customerId}/devices/{deviceId}";
private final VisitService visitService;
//POST http://visits-service/api/customers/{customerId}/devices/{deviceId}/visits
@PostMapping(value = "/visits", produces = MediaType.APPLICATION_JSON_VALUE)
public Mono<Visit> save(@PathVariable("customerId") @Validated long customerId,
@PathVariable("deviceId") @Validated long deviceId,
@RequestBody VisitRequest visitRequest) {
log.info("save {} for customer_id: {}, device_id {}", visitRequest, customerId, deviceId);
return visitService.save(createEntityFromDto(customerId, deviceId, visitRequest))
.switchIfEmpty(error(new RuntimeException("Bad request for save visit:" + visitRequest)));
}
}
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = VisitApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class VisitControllerTest {
@LocalServerPort
private int port;
@Test
void save() throws Exception {
WebTestClient
.bindToServer()
.baseUrl("http://localhost:" + port)
.build()
.post()
.uri("/api/customers/" + ONE.getCustomerId()
+ "/devices/" + ONE.getDeviceId() + "/visits")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.bodyValue(new VisitRequest("test"))
.exchange()
.expectStatus().isCreated()
.expectHeader().valueEquals("Content-Type", MediaTypes.ALPS_JSON_VALUE);
}
}
我的控制器:
@AllArgsConstructor
@Data
public class VisitRequest {
private String description;
public static Visit createEntityFromDto(Long customerId, Long deviceId, VisitRequest visitRequest) {
return new Visit(null, customerId, deviceId, visitRequest.getDescription(),
null);
}
}
@RestController
@RequestMapping(value = VisitController.REST_URL)
@AllArgsConstructor
@Slf4j
public class VisitController {
static final String REST_URL = "/api/customers/{customerId}/devices/{deviceId}";
private final VisitService visitService;
//POST http://visits-service/api/customers/{customerId}/devices/{deviceId}/visits
@PostMapping(value = "/visits", produces = MediaType.APPLICATION_JSON_VALUE)
public Mono<Visit> save(@PathVariable("customerId") @Validated long customerId,
@PathVariable("deviceId") @Validated long deviceId,
@RequestBody VisitRequest visitRequest) {
log.info("save {} for customer_id: {}, device_id {}", visitRequest, customerId, deviceId);
return visitService.save(createEntityFromDto(customerId, deviceId, visitRequest))
.switchIfEmpty(error(new RuntimeException("Bad request for save visit:" + visitRequest)));
}
}
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = VisitApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class VisitControllerTest {
@LocalServerPort
private int port;
@Test
void save() throws Exception {
WebTestClient
.bindToServer()
.baseUrl("http://localhost:" + port)
.build()
.post()
.uri("/api/customers/" + ONE.getCustomerId()
+ "/devices/" + ONE.getDeviceId() + "/visits")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.bodyValue(new VisitRequest("test"))
.exchange()
.expectStatus().isCreated()
.expectHeader().valueEquals("Content-Type", MediaTypes.ALPS_JSON_VALUE);
}
}
运行测试时,WebTestClient返回:
> POST http://localhost:63028/api/customers/10000/devices/20000/visits
> WebTestClient-Request-Id: [1]
> Content-Type: [application/json]
> Accept: [application/json]
> Content-Length: [22]
{"description":"test"}
< 400 BAD_REQUEST Bad Request
< Content-Type: [application/json]
< Content-Length: [169]
{"timestamp":"2020-09-27T19:28:09.987+00:00","path":"/api/customers/10000/devices/20000/visits","status":400,"error":"Bad Request","message":"","requestId":"42d54765-1"}
>帖子http://localhost:63028/api/customers/10000/devices/20000/visits
>WebTestClient请求Id:[1]
>内容类型:[应用程序/json]
>接受:[应用程序/json]
>内容长度:[22]
{“说明”:“测试”}
<400错误请求错误请求
当我在IntelliJ IDEA中的save method控制器的日志行上放置断点时,当我在debug中运行测试时,控制器在到达断点之前返回一个错误的请求
请帮助我查找错误原因使用@PostMapping(value=REST\u URL)代替@PostMapping(value=“/visions”)使用@PostMapping(value=REST\u URL)代替@PostMapping(value=“/visions”)