Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/json;charset=UTF-8' not supported

The error show up when My code like this:

@Test
public void getTemplateByIdTest() throws Exception {
    client.get().uri("/template/getTemplate/7")
            .exchange()
            .expectStatus().isOk()
            .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
            .expectBody(VtTemplateVO.class)
            .returnResult();
}

When I change my code like this,it's ok!

@Test
public void getTemplateByIdTest() throws Exception {
    client.get().uri("/template/getTemplate/7")
            .exchange()
            .expectStatus().isOk()
            .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
            .expectBody(String.class)
            .returnResult();
}

Why when I use .expectBody(VtTemplateVO.class) it will say org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/json;charset=UTF-8' not supported

Somebody knows? please help,thanks

like image 911
Anderson Feng Avatar asked Jul 25 '18 08:07

Anderson Feng


1 Answers

I was also facing this issue caused by Jackson.There needs to be a default constructor for the VtTemplateVO.class with annotated properties. Ex. what I did-

@JsonCreator
public Comment(@JsonProperty("id")String id, @JsonProperty("text")String text, @JsonProperty("createdTime") LocalDate createdTime) {
        this.id = id;
        this.text = text;
        this.createdTime = createdTime;
}

Hope it works for you as well. :)

like image 173
Aparajita Avatar answered Oct 10 '22 17:10

Aparajita