其他分享
首页 > 其他分享> > restTemplate请求发送模板

restTemplate请求发送模板

作者:互联网

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Map;

@Component
public class HttpService {

    @Resource
    private RestTemplate restTemplate;

    public HttpEntity<Map<String, String>> generatePostJson(Map<String, String> jsonMap) {
        HttpHeaders httpHeaders = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        httpHeaders.setContentType(type);
        return new HttpEntity<>(jsonMap, httpHeaders);
    }

    public String sendJsonPost(String uri, Map<String, String> jsonMap) {
        ResponseEntity<String> apiResponse = restTemplate.postForEntity(uri, generatePostJson(jsonMap), String.class);
        return apiResponse.getBody();
    }

    public String sendPost(String uri, String param) {
        ResponseEntity<String> apiResponse = restTemplate.postForEntity(uri, param, String.class);
        return apiResponse.getBody();
    }
}

  

标签:http,String,restTemplate,springframework,发送,jsonMap,org,import,模板
来源: https://www.cnblogs.com/xxcbz/p/16065520.html