gpt4 book ai didi

java - MockMvc 抛出 HttpMediaTypeNotSupportedException 状态预期为 : 201 but was 415

转载 作者:行者123 更新时间:2023-12-01 18:34:02 25 4
gpt4 key购买 nike

我正在测试 api 端点,MockMvc 抛出 org.springframework.web.HttpMediaTypeNotSupportedException 预期状态:<201> 但为:<415>阅读有关类似问题的所有 SO - 解决方案始终是内容类型,但在这种情况下不是)奇怪的是,我在 MockHttpServletResponse 的 headers application/json 中看不到。

Controller :

public static final String MEDIA_TYPE_APPLICATION_JSON_UTF8 = "application/json;charset=utf-8";
@PostMapping(value = "/api/Register", consumes = MEDIA_TYPE_APPLICATION_JSON_UTF8, produces = MEDIA_TYPE_APPLICATION_JSON_UTF8)
public ResponseEntity<Map<String, String>> register(@RequestBody Map<String, String> jsonMap) {
...
Map<String, String> responseMap = new HashMap<>();
MediaType MEDIA_TYPE_JSON_UTF8 = new MediaType("application", "json", StandardCharsets.UTF_8);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MEDIA_TYPE_JSON_UTF8));
headers.setContentType(MEDIA_TYPE_JSON_UTF8);
...
return new ResponseEntity<>(new Gson().toJson(responseMap), headers, HttpStatus.CREATED);
}

测试:

@Test
void Register_phoneNumber_returnOk() throws Exception {
Map<String, String> body = new HashMap<>();
body.put("phone", "1112223344");
Gson gson = new Gson();
String json = gson.toJson(body);
MockHttpServletRequestBuilder request = post("/api/Register");
request.content("{\"phone\":\"1112223344\"}");
request.accept(MEDIA_TYPE_APPLICATION_JSON_UTF8);
request.contentType(MEDIA_TYPE_APPLICATION_JSON_UTF8);
mockMvc.perform(request)
.andDo(print())
.andExpect(status().isCreated());
}

错误:

WARN 6188 --- [main] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=utf-8' not supported]

MockHttpServletRequest:
HTTP Method = POST
Request URI = /api/Register
Parameters = {}
Headers = [Content-Type:"application/json;charset=utf-8", Accept:"application/json;charset=utf-8"]
Body = {"phone":"1112223344"}
Session Attrs = {}

Handler:
Type = ru.controllers.MainController
Method = ru.controllers.MainController#Register(Map)

Async:
Async started = false
Async result = null

Resolved Exception:
Type = org.springframework.web.HttpMediaTypeNotSupportedException

ModelAndView:
View name = null
View = null
Model = null

FlashMap:
Attributes = null

MockHttpServletResponse:
Status = 415
Error message = null
Headers = [Vary:"Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers", Accept:"application/octet-stream, text/plain, application/xml, text/xml, application/x-www-form-urlencoded, application/*+xml, multipart/form-data, multipart/mixed, */*"]
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []


java.lang.AssertionError: Status expected:<201> but was:<415>

最佳答案

答案很简单。

测试类中带有@MockBean注释的服务正在创建实体并将它们保存到数据库,然后 Controller 返回实体id,这在entity.getId()中执行了NPE当然,还有剩余 Controller 测试方法中的 HTTP 415。同时在真实数据库中测试时,postman没有显示错误,因为服务层总是在.getId()方法中返回真实id。在我修改服务层代码以检查实体是否为空后,问题就消失了。

关于java - MockMvc 抛出 HttpMediaTypeNotSupportedException 状态预期为 : 201 but was 415,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60098806/

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