Spring,JSON输入调用另一个应用程序,不发送值

时间:2017-08-30 07:43:48

标签: json spring

我有一个需要与其他应用程序通信的应用程序。

在第一个应用程序中,我提到它:

public List<ContactsBO> getZingyContactsByCompanyId(int companyId) throws Exception {
    String url="http://localhost:8080/ZingyApp/getContactsToBSAByAccountId";
    URL object=new URL(url);
    JSONObject account = new JSONObject();
    String accountId = "58fd8c58abced355f69bf0bb";
    account.put("accountId", accountId);
    HttpURLConnection conn = (HttpURLConnection) object.openConnection();
    conn.setConnectTimeout(50000);
    conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");

    OutputStream os = conn.getOutputStream();
    OutputStreamWriter wr = new OutputStreamWriter(os);
    wr.write(account.toString());
    wr.flush();
    os.close();

    String json_response = "";
    BufferedReader br = null;

    if (conn.getResponseCode() == 200) {
        br = new BufferedReader(new
        InputStreamReader(conn.getInputStream()));
    } else {
        br = new BufferedReader(new
        InputStreamReader(conn.getErrorStream()));
    }
    String text = "";
    while ((text = br.readLine()) != null) {
        json_response += text;
    }
    String contacts = json_response;
    System.out.println("Contacts:"+contacts);

    return null;
}

在第二个应用程序中,我试图获取参数accountId,我将其设置为JSON个对象。

@RequestMapping(value="/getContactsToBSAByAccountId",method=RequestMethod.POST)
public @ResponseBody List<Contacts> getContactsByAccountId(@ModelAttribute("accountId") String accountId) {
    System.out.println("Inside Account Details:"+accountId);
    List<Contacts> contacts=salesContactService.getContactsByAccountId("58fd8c58abced355f69bf0bb");
    return contacts;
}

此处accountIdnull

当我更改@RequestParams时,方法本身没有被调用,我得到400 error,当我更改为@ModelAttribute时,只有它被调用但值没有被发布。< / p>

有人可以对此提出建议吗?

1 个答案:

答案 0 :(得分:0)

@RequestBody映射objectMap而不是ModelAttribute并从map中获取字符串accountId =(String)objectMap.get(“accountId”)

这对我有用。