使用restTemplate报400或者415错误

使用restTemplate调用接口的示例代码如下:

使用restTemplate报400或者415错误

提供方的接口如下:

使用restTemplate报400或者415错误

调用后会出现如下错误:

使用restTemplate报400或者415错误

如图所示,接口使用@RequestBody注解,那么传递的参数应该为json格式,因此上述调用方式改为以下即可:

使用restTemplate报400或者415错误

或者:

使用restTemplate报400或者415错误

 

使用restTemplate调用接口需要的配置:

1.在配置文件中注入RestTemplate

@Bean
@LoadBalanced
RestTemplate restTemplate() {
    return new RestTemplate();
}

2.使用时自动注入即可

@Autowired
RestTemplate restTemplate;

注意:注入restTemplate时,不要使用私有的,会报错。

 

 

转载于:https://my.oschina.net/*yi/blog/1863635