gpt4 book ai didi

java - UnsupportedMediaTypeException : Content type 'application/octet-stream' not supported for bodyType=java. util.Map))

转载 作者:行者123 更新时间:2023-12-05 02:43:31 26 4
gpt4 key购买 nike

我在为 Controller 运行 junit 时遇到以下错误。我已经将内容类型设置为 Json,错误仍然相同。任何建议可能是什么问题?

错误是

java.lang.AssertionError: expectation "expectNext({response=employee saved Successfully})" failed (expected: onNext({response=employee saved Successfully}); actual: onError(org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/octet-stream' not supported for bodyType=java.util.Map<java.lang.String, java.lang.String>))

Controller 是

@Slf4j
@Controller
@RequiredArgsConstructor
public class EmployeeController {

private final EmployeeService employeeService;

@PostMapping(path ="/employees",produces = APPLICATION_JSON_VALUE)
public @ResponseBody Mono<Map<String, String>> saveEmployees(@RequestBody List<EmployeeDto> employeeDtos) {
log.info("Received request to save employees [{}]", employeeDtos);
return employeeService.saveEmployee(employeeDtos);
}
}

服务等级如下:

@Service
@Slf4j
@RequiredArgsConstructor
public class EmployeeService {

private final WebClient webClient;

public Mono<Map<String, String>> saveEmployees(List<EmployeeDto> employeeDtos) {
return webClient
.post()
.uri("/create-employees")
.contentType(APPLICATION_JSON)
.bodyValue(employeeDtos)
.retrieve()
.bodyToMono(new ParameterizedTypeReference<Map<String, String>>() {
})
.doOnError(e -> log.error("Failed to save employees {}: {}", employeeDtos, e));

Junit如下:

@Slf4j
@SpringBootTest
class EmployeeServiceTest {

private static final WireMockServer wireMockServer = new WireMockServer(wireMockConfig().dynamicPort());

@Autowired
private ObjectMapper objectMapper;

@Autowired
private EmployeeService employeeService;

@Test
void shouldMakeAPostApiCallToSaveEmployee() throws JsonProcessingException {
var actualemployeeDtos = "....";
var randomEmployeeDto = ...;
wireMockServer.stubFor(post("/create-employees")
.withHeader(CONTENT_TYPE, equalTo(APPLICATION_JSON_VALUE))
.withHeader(ACCEPT, containing(APPLICATION_JSON_VALUE))
.withRequestBody(equalToJson(actualemployeeDtos))
.willReturn(aResponse()
.withStatus(OK.value())
.withBody("{\"response\": \"employee saved Successfully\"}")));

StepVerifier
.create(employeeService.saveEmployee(List.of(randomEmployeeDto)))
.expectNext(singletonMap("response", "employee saved Successfully"))
.verifyComplete();
}
}

最佳答案

经过调试,我发现连响应头都需要设置内容类型,因为 BodyExtractors 类的 readWithMessageReaders() 方法检查内容类型。

.withHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE)

在下面的代码中设置修复了失败的测试用例

@Test
void shouldMakeAPostApiCallToSaveEmployee() throws JsonProcessingException {
var actualemployeeDtos = "....";
var randomEmployeeDto = ...;
wireMockServer.stubFor(post("/create-employees")
.withHeader(CONTENT_TYPE, equalTo(APPLICATION_JSON_VALUE))
.withHeader(ACCEPT, containing(APPLICATION_JSON_VALUE))
.withRequestBody(equalToJson(actualemployeeDtos))
.willReturn(aResponse()
.withHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE)
.withStatus(OK.value())
.withBody("{\"response\": \"Employees saved Successfully\"}")));

StepVerifier
.create(employeeService.saveEmployee(List.of(randomEmployeeDto)))
.expectNext(singletonMap("response", "Employees saved Successfully"))
.verifyComplete();
}

关于java - UnsupportedMediaTypeException : Content type 'application/octet-stream' not supported for bodyType=java. util.Map<java.lang.String, java.lang.String>)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66882714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com