6

I am trying to use WebClient to call by REST another service, but I always get error:

org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/json' not supported

All allocations have same version of dependencies, calling the resources through Postman works fine. The problem is when first application, acting as a proxy (client), tries to call second one (service)

My Server resource:

@RequestMapping(value = "/properties")
@PutMapping(consumes = APPLICATION_JSON_UTF8_VALUE)
@ResponseStatus(CREATED)
public void saveProperty(@Valid @RequestBody PropertyForm form) {
    service.save(new PropertyImpl(form));
}

My Client resource:

WebClient client = WebClient.create(serviceUrl);

Mono<Void> save(PropertyForm form) {
    return client.put()
            .uri("properties")
            .contentType(MediaType.APPLICATION_JSON_UTF8)
            .body(BodyInserters.fromObject(form))
            .retrieve()
            .bodyToMono(Void.class);
}

My build.gradle file:

dependencies {
    compile "org.springframework.boot:spring-boot-starter-reactor-netty:2.0.4.RELEASE"
    compile "org.springframework.boot:spring-boot-starter-web:2.0.4.RELEASE"
    compile "org.springframework:spring-webflux:5.0.4.RELEASE"

    compile "javax.xml.bind:jaxb-api:2.3.0"
}

Am I missing some dependency, to enable JSON contentType? This example is very simple, yet very problematic for me also.

Form model:

class PropertyForm {

    private String group;
    private String key;
    private String value;
    // getters & setters
}

Source: https://gitlab.com/Berilzar/Sandbox

Beri
  • 11,470
  • 4
  • 35
  • 57
  • Interesting, I've recreated your exact environment at it works for me. Can you can provide the PropertyForm class as well? – Maciej Mościcki Sep 16 '18 at 21:14
  • This is worst scenario for me. As it should work in theory from the start. Form is simple Pojo with only getters. I will link the source as soon as I get back from work. – Beri Sep 17 '18 at 04:40
  • You need to have some kind of Object -> Json encoder on your classpath, e.g. Jackson2JsonEncoder which does the encoding from Object to JSON – Kevin Hussey Sep 17 '18 at 06:17
  • Json serilizer is present, as I have perfectly working GET mappings. – Beri Sep 17 '18 at 17:07

1 Answers1

5

I have finally found the answer. The problem was in the send form actually. Scope of the form was package, same as setters/getters. After I have extracted the PropertyForm to API module, and made everything public, it stated to work.

So the solution was to replace form with:

public class PropertyForm {

    private String group;
    private String key;
    private String value;
    // public getters & setters
}

You need to have all arguments constructor or setter with public scope

Beri
  • 11,470
  • 4
  • 35
  • 57
  • Can you please elaborate more on this..? I am also facing the same issue. My WebClient set is working fine when I retrieve the response as String class, but when I have to retrieve the response in a specific Custom Exception class, I am getting "content type 'application/json' not supported for body type webclient". – swanthri Sep 20 '22 at 06:26