gpt4 book ai didi

java - 响应内容类型为 'null' 。放心异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:47:21 28 4
gpt4 key购买 nike

我很难放心。我收到以下错误

java.lang.IllegalStateException: Cannot parse object because no supported Content-Type was specified in response. Content-Type was 'null'.

我的休息 Controller 如下所示

@Slf4j
@RestController
@RequestMapping(ApiUrls.SESSIONS_API)
@RequiredArgsConstructor
public class SessionsApi {

private final SessionsService sessionsService;

@GetMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public List<SessionDto> sessions(@AuthenticationPrincipal CryptoUser user,
@RequestParam(required = false, defaultValue = "false") boolean includeExpired) {
log.info("User sessions of user [id {} username {}]", user.getUserId(), user.getUsername());


return sessionsService.getSessionsOfUser(user, includeExpired);
}

和测试

SessionDto[] sessionDtos = given().mockMvc(mvc)
.log().all()
.header("Accept","application/json")
.get(ApiUrls.SESSIONS_API)
.then()
.extract()
.as(SessionDto[].class);

是我得到的放心日志

Request method: GET
Request URI: http://localhost:8080/api/sessions?includeExpired=true
Proxy: <none>
Request params: includeExpired=true
Query params: <none>
Form params: <none>
Path params: <none>
Headers: Content-Type=application/json
Accept=application/json
Cookies: <none>
Multiparts: <none>
Body: <none>

我尝试在 getmapping 中添加 produces = MediaType.APPLICATION_JSON_VALUE 但无论如何都没有用。我错过了什么吗?线程甚至没有进入 Controller 方法

最佳答案

如果您的内容类型为空,您可以尝试这样的操作

given().mockMvc(mvc)
.log().all()
.header("Accept","application/json")
.get(ApiUrls.SESSIONS_API)
.then()
.contentType(isEmptyOrNullString())//This is where you handle null
.extract()
.as(SessionDto[].class);

此链接也可能有用:https://github.com/rest-assured/rest-assured/issues/636

关于java - 响应内容类型为 'null' 。放心异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50597588/

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