RestTemplate快速入门

// 案例
@Controller
@RequestMapping("api")
public class Api {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("login")
    public Object login(@RequestParam("username")String username, @RequestParam("password")String password) {
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>(1);
        map.add("username",username);
        map.add("password",password);
        ResponseEntity<String> entity = restTemplate.getForEntity("http://localhost:8081/login?username={username}&password={password}", String.class,map);
        String value = entity.getBody();
        System.out.println("value = " + value);
        return value;
    }
}
@RequestMapping("login")
@RestController
public class LoginController {
    @GetMapping
    public Object login(@RequestParam("username")String username,@RequestParam("password")String password) {

        // TODO login 
        System.out.println("username = " + username);
        System.out.println("password = " + password);
        return username + password;
    }
}

@Configuration
public class RestTemplateConfig {

    @Value("${restTemplate.connectTimeOut:15000}")
    private int connectTimeOut;

    @Value("${restTemplate.readTimeOut:5000}")
    private int readTimeOut;

    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        RestTemplate restTemplate = new RestTemplate(factory);
        List<HttpMessageConverter<?>> converterList = restTemplate.getMessageConverters();
        HttpMessageConverter<?> converterTarget = null;
        HttpMessageConverter<?> jacksonConver = null;
        for (HttpMessageConverter<?> item : converterList) {
            if (item.getClass() == StringHttpMessageConverter.class) {
                converterTarget = item;
                break;
            }
        }
        for (HttpMessageConverter<?> item : converterList) {
            if (item.getClass() == MappingJackson2HttpMessageConverter.class) {
                jacksonConver = item;
            }
        }
        if (converterTarget != null) {
            converterList.remove(converterTarget);
        }
        if (jacksonConver != null) {
            converterList.remove(jacksonConver);
        }
        HttpMessageConverter<?> converter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
        HttpMessageConverter<?> jacksonConverter = new MyJacksonConvert(new ObjectMapper());
        converterList.add(converter);
        converterList.add(jacksonConverter);
        return restTemplate;
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(connectTimeOut);
        factory.setReadTimeout(readTimeOut);
        return factory;
    }

}

class MyJacksonConvert extends MappingJackson2HttpMessageConverter {

    public MyJacksonConvert(ObjectMapper objectMapper) {
        super(objectMapper);
        //        驼峰式(AaBbCc)
//        PropertyNamingStrategy.LOWER_CAMEL_CASE;
//        下划线连接单词 (aa_bb_cc,大小写不分)
//        PropertyNamingStrategy.SNAKE_CASE;
//        驼峰式(AaBbCc)
//        PropertyNamingStrategy.UPPER_CAMEL_CASE;
//        短横线连接单词(aa-bb-cc)
//        PropertyNamingStrategy.KEBAB_CASE;
//        无论什么格式,转换成小写输出
//        PropertyNamingStrategy.LOWER_CASE;
//        设置命名策略
        this.objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE);
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值