json - 如何在 Spring Boot 中将 json 对象发送到 REST API
问题描述
我正在尝试将json
对象发送到 API Post。
JSONParser jsonParser = new JSONParser();
Object jsonArray = jsonParser.parse(new FileReader("mdpayload.json"));
Invocation invocation;
ClientBuilder clientBuilder = ClientBuilder.newBuilder()
.hostnameVerifier((hostname, session) -> hostname.equalsIgnoreCase(session.getPeerHost()));
Client client = clientBuilder.sslContext(getSSLContext()).build();
WebTarget target = client.target(adhPutUrl);
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_EMPTY);
mapper.setSerializationInclusion(Include.NON_NULL);
String entityString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json9);
//System.out.println(" Request for M2 :::\n" + json9);
invocation = target.request().headers(getRequestHeaders())
.build("POST", Entity.entity(json9, MediaType.APPLICATION_JSON))
.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
Response adhResponse = invocation.invoke();
System.out.println("Response is "+adhResponse.getStatus());
但我收到以下错误:
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class net.minidev.json.JSONObject, genericType=class net.minidev.json.JSONObject.
解决方案
从技术上讲,您的服务作为请求接收的 application/json 内容没有实现。
正文编写器是您需要在项目中拥有的一个实现,一个实现 JAX-RS 中定义的接口的类。
这是您可以添加的一个有用的库。
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<scope>runtime</scope>
</dependency>
您可以使用一些解决方案和库,这取决于您使用的服务器。
推荐阅读
- google-chrome-extension - 你如何在chrome中获得所有可见的标签?
- sql - 如何仅获取每个部门的最低工资员工的一条记录?DB2
- php - 使用静态函数为表单的选择选项定义数组
- single-page-application - 与身份服务器中的 cookie 和令牌共享身份验证
- audit.net - Audit.NET.MVC 未登录异常
- ios - 使用 Firebase 启动时适用于 iOS 的 Flutter 应用程序崩溃
- c++ - 包括来自文件系统
- android - 如何以编程方式更改 Android 中材质组件文本按钮的样式?
- vue.js - Vuejs axios 禁用向控制台打印 401 错误
- python - 来自 url 的 Python ftech 标题和 pdf 链接