在Spring Boot中从我的服务器调用另一个rest api
我想根据用户的特定请求从后端调用另一个web api。例如,我想调用Google FCMsend message api,在事件中向特定用户发送消息 改装是否有任何方法来实现这一点?如果没有,我怎么做? 下面是一个代码示例,说明如何获取简单对象:在Spring Boot中从我的服务器调用另一个rest api,api,spring-boot,spring-data,retrofit,Api,Spring Boot,Spring Data,Retrofit,我想根据用户的特定请求从后端调用另一个web api。例如,我想调用Google FCMsend message api,在事件中向特定用户发送消息 改装是否有任何方法来实现这一点?如果没有,我怎么做? 下面是一个代码示例,说明如何获取简单对象: private static void getEmployees() { final String uri = "http://localhost:8080/springrestexample/employees.xml"; Rest
private static void getEmployees()
{
final String uri = "http://localhost:8080/springrestexample/employees.xml";
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(uri, String.class);
System.out.println(result);
}
请尝试此解决方案,而不是通过调用另一个API/URI来获取自定义POJO对象详细信息作为输出的字符串。我希望它对如何使用RestTemplate也有明确的帮助 在springboot中,首先我们需要在@Configuration注释类下为restemplate创建Bean。您甚至可以编写一个单独的类并使用@Configuration进行注释,如下所示
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
}
然后,您必须使用服务/控制器下的@Autowired或@Injected定义RestTemplate,无论您在何处尝试使用RestTemplate。使用下面的代码
@Autowired
private RestTemplate restTemplate;
现在,我们将看到如何使用上面创建的RestTemplate从我的应用程序调用另一个api的部分。为此,我们可以使用多种方法,如execute()、getForEntity()、getForObject()等。这里我将代码与execute()示例放在一起。我甚至尝试过另外两种方法,我面临着将返回的LinkedHashMap转换为预期的POJO对象的问题。下面的execute()方法解决了我的问题
ResponseEntity<List<POJO>> responseEntity = restTemplate.exchange(
URL,
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<POJO>>() {
});
List<POJO> pojoObjList = responseEntity.getBody();
ResponseEntity ResponseEntity=restemplate.exchange(
网址,
HttpMethod.GET,
无效的
新的ParameteredTypeReference(){
});
List pojoObjList=responseEntity.getBody();
快乐编码:)为Rest模板创建Bean,以自动连接Rest模板对象
@SpringBootApplication
public class ChatAppApplication {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ChatAppApplication.class, args);
}
}
使用restemplate-exchange()方法使用GET/POST API。下面是控制器中定义的post api
@RequestMapping(value = "/postdata",method = RequestMethod.POST)
public String PostData(){
return "{\n" +
" \"value\":\"4\",\n" +
" \"name\":\"David\"\n" +
"}";
}
@RequestMapping(value = "/post")
public String getPostResponse(){
HttpHeaders headers=new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity=new HttpEntity<String>(headers);
return restTemplate.exchange("http://localhost:8080/postdata",HttpMethod.POST,entity,String.class).getBody();
}
@RequestMapping(value=“/postdata”,method=RequestMethod.POST)
公共字符串PostData(){
返回“{\n”+
“值”:“4\,\n”+
“\”姓名\“:\”大卫\“\n”+
"}";
}
@请求映射(value=“/post”)
公共字符串getPostResponse(){
HttpHeaders=新的HttpHeaders();
setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity=新的HttpEntity(标题);
返回restTemplate.exchange(“http://localhost:8080/postdata,HttpMethod.POST,entity,String.class).getBody();
}
请参阅本教程[1]
[1]
改造是否有任何方法来实现这一点?如果没有,我怎么做
是
改型是针对Android和Java的类型安全REST客户端。改型将HTTP API转换为Java接口
有关更多信息,请参阅以下链接
现代Spring 5+回答时使用而不是
RestTemplate
将特定web服务或资源的WebClient
配置为bean(可以配置其他属性)
@Bean
公共网络客户端localApiClient(){
返回WebClient.create(“http://localhost:8080/api/v3");
}
注入并使用服务中的bean
@服务
公共类用户服务{
私有静态最终持续时间请求\u超时=持续时间秒(3);
私有最终网络客户端localApiClient;
@自动连线
公共用户服务(WebClient LocalApicClient){
this.localApiClient=localApiClient;
}
公共用户getUser(长id){
返回localApiClient
.get()
.uri(“/users/”+id)
.retrieve()
.BodyToNo(用户类)
.block(请求超时);
}
}
正如在这里的各种回答中提到的,WebClient现在是推荐的路线。
您可以从配置WebClient builder开始:
@Bean
public WebClient.Builder getWebClientBuilder(){
return WebClient.builder();
}
然后注入bean,您可以按照如下方式使用API:
@Autowired
private WebClient.Builder webClientBuilder;
Product product = webClientBuilder.build()
.get()
.uri("http://localhost:8080/api/products")
.retrieve()
.bodyToMono(Product.class)
.block();
您不需要第三方库。Spring已经将问题标记为]将在将来的版本中被弃用,请使用更现代的替代对象result=restTemplate.getForObject(uri,Object.class);-更generic@MuhammadFaizanUDdin我考虑过,但是如果对象由于任何原因不能被正确序列化,iirc现在可能会工作;而字符串方法总是有效的,因为JSON总是可以序列化为字符串。在将来的版本中会被弃用,请使用下面更现代的WebClient替代方案。这确实非常好。而且,当我尝试使用几乎完全相同的代码时,我会遇到错误“无法反序列化[my pojo class]的实例”启动外对象令牌。您知道这是为什么吗?请验证您的pojo是否实现了可序列化接口?如果没有实现,请尝试。不幸的是,这并没有解决它,谢谢您。对于正在搜索包含WebClient的包的用户,它是
org.spr中的spring boot starter webflux
ingframework.boot
。必须将其包含在pom.xml文件中。