如何从服务和数据库中获取数据并将post数据发送到服务器?

dfddblmv  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(399)

我使用restemplatejson从服务和数据库获取数据,然后将post发送到服务器。我对 Postman 做了测试,结果是200分
我的格式json:

{
"senderUser": "myemail@gmail.com",
"data": [
    {
        "actionType": "update-contact",
        "data": {
            "name": "NakiTa  ",
            "lastname": "Isumi",
            "type": 0,
            "title": "ms",
            "passport": "123123",
            "gender": 1,
            "dateOfBirth": "03-01-2021",
            "emails": [
                {
                    "value": "test@gmail.com"
                }
            ],
            "phones": [
                {
                    "value": "0902032618"
                }
            ],
            "addresses": [
                {
                    "addressDetail": "Osaka",
                    "street": "Osaka",
                    "city": "Osaka",
                    "state": "JP",
                    "country": {
                        "code": "jp",
                        "name": "Japan"
                    }
                }
            ],
            "cusFields": [
                {
                    "600f9cb0f02f084bd8a3dcdb": "TEST"
                }                    
            ],
            "customerType": "company"               
        }
    }
]
}

我的班级:

@RequestMapping(value = "/getAllCustomerBasic.do", method = RequestMethod.GET)
public ResponseEntity<List<MyClassMap>> getAllCustomerBasic(@RequestParam Map<String, Object> params,
      HttpServletRequest request) throws Exception {

LOGGER.debug("groupCode : {}", params.get("custId"));

RestTemplate restTemplate = new RestTemplate();

MappingJackson2HttpMessageConverter jsonHttpMessageConverter = new MappingJackson2HttpMessageConverter();
jsonHttpMessageConverter.getObjectMapper().configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
restTemplate.getMessageConverters().add(jsonHttpMessageConverter);

String url = "https://myservice/6670011cbd4a674458d3b26025/90336429462601e7f3326641898fabd9948b349d";
List<MyClassMap> customerList = customerService.getAllCustomerViewBasicInfo(params);            // .selectAccBank(params);

try {

    JSONArray arrayParent = new JSONArray();
    for(int i=0 ; i<customerList.size() ; i++) {
        arrayParent.put(customerList.get(i));
    }

    JSONObject objectParent = new JSONObject();
    objectParent.put("data", arrayParent);

    JSONObject objectChild = new JSONObject();
    objectChild.put("senderUser", "myemail@gmail.com");

    JSONObject objectChildData = new JSONObject();
    objectChildData.put("actionType", "update-contact");

    HttpHeaders headers = new HttpHeaders();
    headers.add("Accept", MediaType.APPLICATION_JSON.toString());
    headers.add("Content-Type", MediaType.APPLICATION_JSON.toString());

    HttpEntity<String> entity = new HttpEntity<String>(objectParent.toString(), headers);
    String result = restTemplate.postForObject(url, entity, String.class);

    LOGGER.info(result);
    LOGGER.info("header: " + entity.getHeaders());
    LOGGER.info("body" + entity.getBody());
}

catch (HttpClientErrorException exception) {
    // TODO Auto-generated catch block
    exception.printStackTrace();
}

return ResponseEntity.ok(customerList);         // customerList   jSONObject
}

customerservice将成功地从数据库获取数据,但当我构建和检查数据时,似乎会发生以下异常:

body{"data":{}}

似乎get data为空,无法获取所有数据。如何解决这个问题?非常感谢

zvokhttg

zvokhttg1#

resttemplate自动将序列化为json。您只需定义一个java类,该类具有api所期望的相同属性(并且具有相同类型),并且在postforobject方法中指示该对象与您的类相同,而不是string.class。
例如,如果myclassmap数据与data json数组中的元素具有相同的属性,则可以是:

//You have to add getters and setters and constructor
public Class Request {
  private String senderUser;
  private List<MyClassMap> data;
}

ClassRequest request= new ClassRequest();
request.setData(customerList);
request.setSenderUser("example");
HttpEntity<ClassRequest> entity = new HttpEntity<ClassRequest>(request, headers);

String result = restTemplate.postForObject(url, entity, ClassRequest.class);

相关问题