javarestspring-bootspring-webfluxspring-web

Spring WebClient put Mapping: Content type 'application/json' not supported


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


Solution

  • 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