如何在春季启动时将JSON对象发送到REST API

时间:2019-08-06 02:30:43

标签: json spring-boot

我正在尝试将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.

1 个答案:

答案 0 :(得分:0)

从技术上讲,您的服务正在作为请求接收的application / json内容没有实现。

body writer是项目中需要的一种实现,它是一种实现JAX-RS中定义的接口的类。

这是您可以添加的一个有用的库。

<dependency>
  <groupId>org.glassfish.jersey.media</groupId>
  <artifactId>jersey-media-json-jackson</artifactId>
  <scope>runtime</scope>
</dependency>

您可以使用一些解决方案和库,这取决于您使用的服务器。

相关问题