简体   繁体   中英

How to send data map to a web service?

I've got this code of my web service:

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public static Response fillData(final Map<String, Object> data) {  
       ...  
       final byte[] file = ...
       return Response.ok(file).build();

How do I send the Map data to the service?

final javax.ws.rs.core.Response reponse = client.target(URL_REST).path("/path").request(MediaType.APPLICATION_JSON).post(?);

Here is a sample of my JSON file:

{
  "activity" : {
    "code" : "ACT_014",
    "title" : "FIGHTING"
  },
  "adress" : {
    "place" : "",
    "number" : ""
  }
}

Thank you.

Have you tried via HttpURLConnection?

URL url = new URL(URL_REST);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");

        String input = "{\"activity\" :....}";

        OutputStream os = conn.getOutputStream();
        os.write(input.getBytes());
        os.flush();

... and collect output response data

我必须使用MediaType.APPLICATION_JSON_TYPE:

.post(Entity.entity(data, MediaType.APPLICATION_JSON_TYPE), Response.class)

如果您知道主机地址,则U也可以使用邮递员,curl或soapUI。

If you haven't requirements for the technology of the client side, you can use the Restlet framework and the library org.json . Here is below a sample of code:

ClientResource cr = new ClientResource("http://...");
JSONObject obj = new JSONObject();
obj.put("activity", "my value");
(...)
cr.post(new JsonRepresentation(obj));

Hope it helps, Thierry

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