简体   繁体   中英

How to send json object to REST API in spring boot

I am trying to send json object to 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());

But i am getting bellow error:

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.

Technically there is no implementation for the application/json content that your service is receiving as a request.

The body writer is a implementation you need to have in your project, one class that implements the interface defined in JAX-RS.

Here is one usefull library you can add.

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

There are some solutions and libraries you can use, it depends the server you are using.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM