Spring WebClient put 映射:不支持内容类型“application/json”

Ber*_*eri 6 java rest spring-boot spring-web spring-webflux

我尝试使用 WebClient 通过 REST 调用另一个服务,但总是收到错误:

org.springframework.web.reactive.function.UnsupportedMediaTypeException:不支持内容类型“application/json”

所有分配都具有相同版本的依赖项,通过 Postman 调用资源效果很好。问题是当第一个应用程序充当代理(客户端)尝试调用第二个应用程序(服务)时

我的服务器资源:

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

我的客户资源:

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);
}
Run Code Online (Sandbox Code Playgroud)

我的 build.gradle 文件:

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"
}
Run Code Online (Sandbox Code Playgroud)

我是否缺少一些依赖项来启用 JSON contentType?这个例子很简单,但对我来说也很成问题。

表格型号:

class PropertyForm {

    private String group;
    private String key;
    private String value;
    // getters & setters
}
Run Code Online (Sandbox Code Playgroud)

来源: https: //gitlab.com/Berilzar/Sandbox

Ber*_*eri 5

我终于找到了答案。问题实际上出在发送表单上。表单的范围是包,与 setter/getters 相同。在我将 PropertyForm 提取到 API 模块并将所有内容公开后,它表明可以工作。

所以解决方案是将 form 替换为:

public class PropertyForm {

    private String group;
    private String key;
    private String value;
    // public getters & setters
}
Run Code Online (Sandbox Code Playgroud)

您需要将所有参数构造函数或设置器设置为公共范围