UnsupportedMediaTypeException возникает при использовании WebClient для использования RESTful API - PullRequest
0 голосов
/ 01 августа 2020

Я использую WebClient для использования iTunes Search API и хочу получить некоторую информацию из ответа. Здесь я создаю поисковый запрос для альбома на основе keyword.

WebClient webClient = WebClient.create("https://itunes.apple.com/search");

Mono<AlbumResponse> albums = webClient.get()
                .uri(uriBuilder -> uriBuilder
                        .queryParam("entity", "album")
                        .queryParam("term", "modonna")
                        .build())
                .accept(MediaType.APPLICATION_JSON)
                .retrieve()
                .bodyToMono(AlbumResponse.class);

AlbumResponse result = albums.block();

Я создал класс AlbumResponse:

@JsonIgnoreProperties(ignoreUnknown = true)
public class AlbumResponse {
    private String resultCount;
    private List<Album> results;

    public String getResultCount() {
        return resultCount;
    }

    public void setResultCount(String resultCount) {
        this.resultCount = resultCount;
    }

    public List<Album> getResults() {
        return results;
    }

    public void setResults(List<Album> results) {
        this.results = results;
    }
}

И класс Album:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Album {
    private String collectionName;
    private List<String> artistName;
    private String collectionType;

    public Album(){
        artistName = new ArrayList<>();
        collectionType = "Album";
    }

    public String getCollectionName() {
        return collectionName;
    }

    public void setCollectionName(String collectionName) {
        this.collectionName = collectionName;
    }

    public List<String> getArtistName() {
        return artistName;
    }

    public void setArtistName(List<String> artistName) {
        this.artistName = artistName;
    }

    public String getCollectionType() {
        return collectionType;
    }

    public void setCollectionType(String collectionType) {
        this.collectionType = collectionType;
    }
}

Однако, когда я запускаю приведенный выше код. У меня всегда возникала ошибка при выполнении метода block(). Сообщение об ошибке:

Caused by: org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'text/javascript;charset=utf-8' not supported for bodyType=com.kramphub.demo.model.AlbumResponse
    at org.springframework.web.reactive.function.BodyExtractors.lambda$readWithMessageReaders$12(BodyExtractors.java:201) ~[spring-webflux-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
    |_ checkpoint ⇢ Body from GET https://itunes.apple.com/search?entity=album&term=modonna [DefaultClientResponse]

Кто-нибудь знает причину, по которой я получаю эту ошибку?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...