如何通过客户端的post方法使用json对象 [英] How to consume a json object via post method from client

查看:77
本文介绍了如何通过客户端的post方法使用json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个json对象,我需要通过post将其传递给我的服务器.json在我的客户端内部进行了硬编码.以下是我的服务器程序(test.java)-请注意,这是出于学习目的.因此,请清楚解释...

I have a json object and i need topass it to my server via post.The json is hard coded inside my client. Below is my server program(test.java) - please note this is for learning purpose. So explain it clearly...

@Path("/json/product")
    public class Test {


        @POST
        @Path("/post")

        @Consumes(MediaType.APPLICATION_JSON)
        public Response createProductInJSON() {

            return Response.status(201).entity(....).build();



        }
      }

如何将json从客户端传递到服务器?

how json is gets passed from client to server?

我关注了 http://www .mkyong.com/webservices/jax-rs/restful-java-client-with-jersey-client/教程(对post方法感到困惑). 在客户端程序中,需要通过硬方法编码的json格式来传递并通过post方法接收...在服务器上. json可以作为参数从客户端传递到服务器吗? 否则如何做... 下面是我的示例客户端程序....

I followed http://www.mkyong.com/webservices/jax-rs/restful-java-client-with-jersey-client/ tutorial by mkyong(confused about post method). From a client program a hard coded json format needed to pass and recieved via post method...at server. Can the json pass from client to server as parameters...? or else how to do it.... below is my sample client program....

public static void main(String[] args) {

          try {

            URL url = new URL("http://localhost:8080/Snapshothealthapp1/rest/json/product/post");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            String JSON_DATA =
                     "{" 
                       + "  \"SnapshotRequest\": [" 
                       + "    {" 
                       + "      \"AuthenticationType\": \"email\"," 
                       + "      \"EmailAddress\": \"test@gmail.com\","                  
                       + "      \"Password\" : \"12345\"," 
                       + "      \"PracticeID\" : \"null\"," 
                       + "      \"DeviceID\" : \"null\""
                       + "    } +   ]"
                   + "}";
//          request.body("application/json", JSON_DATA);
//          String input = "{\"singer\":\"Metallica\",\"title\":\"Fade To Black\"}";

            OutputStream os = conn.getOutputStream();
            os.write(JSON_DATA.getBytes());
            os.flush();
            if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
                throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (conn.getInputStream())));

            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }

            conn.disconnect();

          } catch (MalformedURLException e) {

            e.printStackTrace();

          } catch (IOException e) {

            e.printStackTrace();

         }

我需要将字符串json数据传递到我的服务器.任何人都可以为此提供客户端和服务器代码吗?

I need to pass the string json data to my server.Can anyone please give a client and server code for this?

预先感谢

推荐答案

将@Consumes添加为"application/json",并在JsonObject Type中指定method参数.您必须添加了javax.json.jar(通常随 JAX-RS ).

Add @Consumes as "application/json" and specify the method parameter in JsonObject Type. You must have added javax.json.jar (normally comes with JAX-RS ) .

@Path("/create")
@POST
@Consumes (MediaType.APPLICATION_JSON) //Specify the POST MediaType as "application/json"
@Produces(MediaType.APPLICATION_JSON)
public Book createBook(JsonObject postData //Consumable JSON Object param  ) {

     System.out.println( postData); //This prints the client's JSON object from the Request Body

     //Do your work here with the postData ..........
     String value1 = postData.getString("key1");
     int value2 = postData.getInt("key2");
     //................
     //........
     //.....
}

这篇关于如何通过客户端的post方法使用json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆