java - Spring WebClient 放置映射 : Content type 'application/json' not supported

标签 java rest spring-boot spring-webflux spring-web

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

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

所有分配都有相同版本的依赖项,通过 Postman 调用资源工作正常。问题是第一个应用程序作为代理(客户端)尝试调用第二个应用程序(服务)

我的服务器资源:

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

我的客户资源:

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);
}

我的 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"
}

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

表单模型:

class PropertyForm {

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

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

最佳答案

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

因此解决方案是将 form 替换为:

public class PropertyForm {

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

您需要让所有参数的构造函数或 setter 具有公共(public)范围

关于java - Spring WebClient 放置映射 : Content type 'application/json' not supported,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52357898/

相关文章:

java - Android Fragment OnAttach方法

java - 纯 Java Spring Servlet 配置中没有类型的合格 Bean

java - 使用 onFling 更改 Imagebutton 图像

java - 在后端使用常量列表服务或在前端应用程序中声明常量

java - Spring Boot 和 PostgreSQL : Error creating bean with name 'entityManagerFactory' defined in class path resource

java - 如何从文本文件读取到对象数组

java - 如何在 REST Web 服务中使用 json 生成 http 响应?

tomcat - 为什么 Tomcat 会以不同方式解析某些 url 映射的上下文路径?

spring-boot - Spring Boot App容器无法连接到MySQl容器

java - spring-boot、JPA 和 CrudRepository 出现@autowire 错误