java应用程序中的一个方法。哪个方法应该从Rest Api返回数据

时间:2018-05-17 13:18:08

标签: java spring rest spring-boot

我在JavaFX和spring boot中有一个应用程序。 Spring Boot中的另一个Web应用程序。从Web应用程序,我可以通过rest API访问Fx应用程序。但是现在我想在java Fx应用程序中调用一个方法。哪个方法从Web应用程序返回数据为String。我该怎么办?

 public  String getCustomerData(){

/* RestUrl Of web application */
    String url="http://192.168.012.106:8080/restrole/customer/get/1?access_token=e91ad118-141a6026b954";

    MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
    headers.add("HeaderName", "value");
    headers.add("Content-Type", "application/json");
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

    HttpEntity<Customers> request = new HttpEntity<>(Customers, headers);

    restTemplate.postForObject(url, request, Customers.class);

    /* here I want to catch the JSON data and return it.*/
    return null;

}

1 个答案:

答案 0 :(得分:0)

protected ResponseEntity<AllCustomerTypeMessage> getCustomerData(String token) {
    try {

        String url = "http://192.168.012.106:8080/restrole/customer/get?access_token=" + token;

        HttpHeaders headers = new HttpHeaders();
        headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);
        RestTemplate restTemplate = new RestTemplate();

        ResponseEntity<AllCustomerTypeMessage> allCustomerType = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<Object>(headers), AllCustomerTypeMessage.class);


        return allCustomerType;
    } catch (Exception e) {
        // Log.e(TAG,e.getMessage(),e);
    }
    return null;
}