服务间RestTemplate 调用 及 获取请求头部参数信息方法

本文介绍如何使用Spring框架的RestTemplate进行服务间调用,包括设置请求头、解析响应等关键步骤,以及一个完整的OCR认证接口调用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过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);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值