我有放心的问题。我收到以下错误

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

我的休息控制器如下图所示
@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,但是无论如何都无法正常工作。我想念什么吗?线程甚至没有进入控制器方法

最佳答案

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

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 - 响应内容类型为“空”。放心的异常(exception),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50597588/

10-10 12:53