服務間RestTemplate 呼叫 及 獲取請求頭部引數資訊方法
阿新 • • 發佈:2018-12-31
通過RestTemplate 實現服務間呼叫
@Autowired
RestTemplate restTemplate;
ResponseEntity tokenResp = restTemplate.postForEntity(“http://osg-uc0003/member/c7/f01”, params,
String.class);
獲取請求頭部引數資訊
@RequestHeader(value=“md5”) String md5,@RequestHeader(value=“timeStamp”) String timeStamp
使用例項
import javax.servlet. http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework. http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client. RestTemplate;
import com.sgcc.feign.OcrAuthFeign;
@RefreshScope
@RestController
@RequestMapping(value = "/auth")
public class ExpandController extends BaseModelController {
private static final Log log = LogFactory.getLog(ExpandController.class);
@Autowired
HttpServletRequest request;
@Autowired
RestTemplate restTemplate;
/**
* OCR認證介面
* @param paramData
*/
@RequestMapping(value = "/**")
@ResponseBody
public void test(@RequestHeader(value="md5") String md5,@RequestHeader(value="timeStamp") String timeStamp,@RequestBody String paramData) {
String url = request.getRequestURL().toString();
url = url.substring(url.indexOf("auth/")).replaceFirst("auth/", "http://osg-uc0005/");
log.info("OCR認證介面,請求引數:" + paramData);
String result = restTemplate.postForEntity(url, paramData,
String.class).getBody().toString();
log.info("OCR認證介面,返回引數:" + result);
this.renderText(result);
}
}