RestTemplate .get()和.post()

时间:2018-08-31 18:31:35

标签: java rest soap

我正在从SOAP转换为REST。我有两个不同的测试电话。我已经弄清楚了uri和标头,但是我不知道的是.get()和.post()。我需要在REST中使用什么来完成此任务?

public ResponseEntity<String> getGetResponse(URI uri, String wamUsername) {     
    return new Request().uri(uri)
                        .header("Accept", "application/hal+json")
                        .header("Authorization", authHeaders.getAuth())
                        .header("wam-authenticated-username", wamUsername)
                        **.get(String.class)**;
}

public ResponseEntity<?> getPostResponse(URI uri, String wamUsername, Object obj) {
    return new Request().uri(uri)
                        .header("Accept", "application/hal+json")
                        .header("Authorization", authHeaders.getAuth())
                        .header("wam-authenticated-username", wamUsername)
                        **.post(obj)**;
}

1 个答案:

答案 0 :(得分:0)

public ResponseEntity<String> getGetResponse(URI uri, String wamUsername) {   

    RestTemplate restTemplate = new RestTemplate();

    HttpHeaders headers = new HttpHeaders();
    headers.add("Accept", "application/hal+json");
    headers.add("Authorization", authHeaders.getAuth());
    headers.add("wam-authenticated-username", wamUsername);
    HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);

    return restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);

}

public ResponseEntity<?> getPostResponse(URI uri, String wamUsername, Object obj) {

    RestTemplate restTemplate = new RestTemplate();

    HttpHeaders headers = new HttpHeaders();
    headers.add("Accept", "application/hal+json");
    headers.add("Authorization", authHeaders.getAuth());
    headers.add("wam-authenticated-username", wamUsername);
    HttpEntity<Object> entity = new HttpEntity<Object>(obj, headers);

    return restTemplate.exchange(uri, HttpMethod.POST, entity, Object.class);

}