RestTemplate 例子

public class RestClient {
    private static final RestTemplate temp = new RestTemplate();
    
    private static void validateDeal() throws Exception {


        HttpEntity entity = ClientHelper.getHttpEntity(form.toJsonString());
        
        ResponseEntity output = temp.postForEntity("url_string", entity, String.class);
        List results = JsonConverter.convert(output.getBody(), new TypeReference>(){});
        
        System.out.println(results);
    }
    
    private static Portfolio getPortfolio(String uid, String code) throws Exception {
        ResponseEntity output = temp.exchange("/test_url?uid=" + uid + "&code=" + code), HttpMethod.GET, ClientHelper.getHttpEntity(String.class), String.class);
        List results = JsonConverter.convert(output.getBody(), new TypeReference>(){});
    }

}

--------------------------------------------------------简朴的分割线--------------------------------------------------------

public final class ClientHelper {
    public static HttpEntity getHttpEntity(T object){
        HttpEntity entity = new HttpEntity(object, getHeaders());
        return entity;
    }
    
    public static HttpEntity getHttpEntity(Class clz){
        HttpEntity entity = new HttpEntity(getHeaders());
        return entity;
    }
}


你可能感兴趣的:(Code,Snippet)