繁体   English   中英

通过pojo对象和方法发布对Web服务的请求宁静

[英]Request to web services restful with pojo object and method post

我创建了一个宁静的Web服务:

@POST
@Path("grd")
@Consumes("application/json")
@Produces("text/plain")
public String guardarDato(PostParams jsonObject) {
/*
 something here
}

PostParams是一个pojo:

public class PostParams {

    private final  Map<String, String> postParamsList;

    public PostParams() {
        postParamsList = new HashMap<>();
    }

    public void addParameter(String name, String value) {
        postParamsList.put(name, value);
    }

    public String getPostParamsList(String name) {
        return postParamsList.get(name);
    }

    public void getPostParamsMap() {
        if (postParamsList.isEmpty()) {
            System.out.println("Parametros vacios");
        } else {
            for (String key : postParamsList.keySet()) {
                System.out.println("Clave: " + key + " -> Valor: " +         postParamsList.get(key));
            }
        }
    }
}

我正在尝试使用HttpUrlConnection从android调用此Web服务,但是我的对象在我的Web服务中为null pojo。

 try {

        URL url = new URL("http://localhost:8080/VfqCh/hgt/opli/grd");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
       // conn.setRequestProperty("Accept", "application/json");

        PostParams postParams = new PostParams();
        postParams.addParameter("h", "51rt3");
        postParams.addParameter("x", "dsfsd8698sdfs");
        postParams.addParameter("ax", "Dairon");
        postParams.addParameter("tf", "D");
        // String input = "{\"qty\":100,\"name\":\"iPad 4\"}";
        Gson gson = new Gson();
        String gString = gson.toJson(postParams, PostParams.class);
        try (OutputStreamWriter printout = new OutputStreamWriter(conn.getOutputStream())) {
            printout.write(gString);
            printout.flush();
        }

        if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
            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();

    }

}

该信息将发送到Web服务,但该对象为null。 如果我更改了Web服务中接收的对象的类型(如果可以的话):

@POST
@Path("grd")
@Consumes("application/json")
@Produces("text/plain")
public String guardarDato(String jsonObject) {
/*
 something here
}

但是我需要在Web服务中接收对象! 有可能的?

您为HashMap制作了POJO,但是您可能只使用了Gson已经提供的JsonObject ,并且基本上像HashMap一样工作。 然后,您将其toString ,并具有可发送到服务器的JSON字符串。 不需要Gson.toJson ,也不必担心它是否正确转换。

您的服务器正在接受JSON字符串,定义如下

@Consumes("application/json")

因此,这应该是方法

public String guardarDato(String jsonObject) {

您将需要将Gson添加为服务器的依赖项,然后您应该能够使用诸如PostParams params = Gson.fromJson(jsonObject, PostParams.class)类的东西将JSON获取到对象,假设数据是从客户端正确发送的方面,但是如前所述,您的POJO不会在JsonObject或纯HashMap之上添加太多功能

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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