Springboot HTTP如何调用其他服务

 更新时间:2022年01月28日 10:09:44   作者:静忻寒  
这篇文章主要介绍了Springboot HTTP如何调用其他服务,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

HTTP如何调用其他服务

1.GET请求

1.1Client代码

import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
 
@Service
public class UserInfoClient {
    public String getUserTotalAmount(){
        Map<String,String> map=new HashMap<String,String>();
        map.put("name","123");
        map.put("password","123");
        URI uri = UriComponentsBuilder.fromHttpUrl("http://localhost:8081/spring/test")
                .queryParam("jsonString",JSON.toJSONString(map))
                .queryParam("token","12122222111")
                .build().encode().toUri();
        RestTemplate restTemplate=new RestTemplate();
        String data=restTemplate.getForObject(uri,String.class);
        System.out.println(data);
        return null;
    }
    public static void main(String[] args){
        UserInfoClient c=new UserInfoClient();
        c.getUserTotalAmount();
    }
}

1.2 Service 代码

import org.springframework.web.bind.annotation.*; 
@RestController
@RequestMapping(value = "/spring")
public class Test {
    @RequestMapping(value = "/test",method = RequestMethod.GET)
    public String testSpringBoot(@RequestParam String jsonString,@RequestParam String token){
        System.out.println(jsonString);
        System.out.println(token);
        /*
         *逻辑处理
         */
        return "Spring Boot 测试成功!";
    }
}

2.POST请求

2.1Client代码

import com.alibaba.fastjson.JSON;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
 
@Service
public class UserInfoClient { 
    public String getUserTotalAmount(){
        Map<String,String> map=new HashMap<String,String>();
        map.put("name","123");
        map.put("password","123");
        String url="http://localhost:8081/spring/test";
        //设置请求头信息
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        //设置body部分
        HttpEntity<String> entity = new HttpEntity<String>(JSON.toJSONString(map),headers);
        RestTemplate restTemplate=new RestTemplate();
        ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
        System.out.println(result.getBody());
        return null;
    }
    public static void main(String[] args){
        UserInfoClient c=new UserInfoClient();
        c.getUserTotalAmount();
    }
}

2.2 Service代码

 
import org.springframework.web.bind.annotation.*;
 
@RestController
@RequestMapping(value = "/spring")
public class Test {
    @RequestMapping(value = "/test",method = RequestMethod.POST)
    public String testSpringBoot(@RequestBody UserBean userBean){
        System.out.println(userBean);
        /*
         *逻辑处理
         */
        return "Spring Boot 测试成功!";
    }
}

springboot请求其他服务器的http接口

使用Get方式,携带headers请求数据

//注入
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/FaceInfo")
@ResponseBody
public Object   faceInfo(String startTime,String endTime,Integer size ){
    String apiURL = "http://www.05un.cn/wspp/GceGroups";
    HttpHeaders headers = new HttpHeaders();
   headers.add("userId","38");
    // headers.set("userId","38");
    headers.setContentType(MediaType.APPLICATION_JSON);
    Map<String, Object> requestParam = new HashMap<>();
    HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(requestParam, headers);
        ResponseEntity<String> entity2 = restTemplate.exchange(apiURL, HttpMethod.GET, request, String.class);
    String body = entity2.getBody();
    return body;
}

使用Post方式,携带body请求数据

//注入
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/FaceInfo")
@ResponseBody
public Object   faceInfo(String startTime,String endTime,Integer size ){
    String apiURL = "http://www.0531yun.cn/wsjc/app/Login";
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    Map<String, Object> requestParam = new HashMap<>();
    requestParam.put("loginName", "jnr");
    requestParam.put("password", "jn");
    HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(requestParam, headers);
    String s=request.toString();
    ResponseEntity<String> entity2 = restTemplate.exchange(apiURL, HttpMethod.POST, request, String.class);
    String body = entity2.getBody();
    return body;
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 解决Springboot get请求是参数过长的情况

    解决Springboot get请求是参数过长的情况

    这篇文章主要介绍了解决Springboot get请求是参数过长的情况,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • Java框架Struts2实现图片上传功能

    Java框架Struts2实现图片上传功能

    这篇文章主要为大家详细介绍了Java框架Struts2实现图片上传功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-08-08
  • java MongoDB实现列表分页查询的示例代码

    java MongoDB实现列表分页查询的示例代码

    本文主要介绍了java MongoDB实现列表分页查询的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • Spring Boot JPA中使用@Entity和@Table的实现

    Spring Boot JPA中使用@Entity和@Table的实现

    这篇文章主要介绍了Spring Boot JPA中使用@Entity和@Table的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03
  • Java多线程之多线程异常捕捉

    Java多线程之多线程异常捕捉

    在java多线程程序中,所有线程都不允许抛出未捕获的checked exception,也就是说各个线程需要自己把自己的checked exception处理掉,通过此篇文章给大家分享Java多线程之多线程异常捕捉,需要的朋友可以参考下
    2015-08-08
  • 使用Java的Spring框架编写第一个程序Hellow world

    使用Java的Spring框架编写第一个程序Hellow world

    这篇文章主要介绍了Java的Spring框架并用其开始编写第一个程序Hellow world的方法,Spring是Java的SSH三大web开发框架之一,需要的朋友可以参考下
    2015-12-12
  • 微信跳一跳辅助Java代码实现

    微信跳一跳辅助Java代码实现

    这篇文章主要为大家详细介绍了微信跳一跳辅助的Java代码实现资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • SpringBoot如何优雅的处理重复请求

    SpringBoot如何优雅的处理重复请求

    对于一些用户请求,在某些情况下是可能重复发送的,如果是查询类操作并无大碍,但其中有些是涉及写入操作的,一旦重复了,可能会导致很严重的后果,所以本文给大家介绍了SpringBoot优雅的处理重复请求的方法,需要的朋友可以参考下
    2023-12-12
  • 5种Java经典创建型模式详解

    5种Java经典创建型模式详解

    这篇文章主要为大家详细介绍了5种Java经典创建型模式,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • Spring Boot与ActiveMQ整合的步骤

    Spring Boot与ActiveMQ整合的步骤

    今天小编就为大家分享一篇关于Spring Boot与ActiveMQ整合的步骤,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01

最新评论