java 单机接口限流处理方案
347
2022-08-31
Springboot HTTP如何调用其他服务
目录HTTP如何调用其他服务1.GET请求2.POST请求springboot请求其他服务器的http接口使用Get方式,携带headers请求数据使用Post方式,携带body请求数据
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
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);
/*
*逻辑处理
*/
returQCpdzNirn "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
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.setConterQCpdzNintType(type);
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
//设置body部分
HttpEntity
RestTemplate restTemplate=new RestTemplate();
ResponseEntity
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://05un.cn/wspp/GceGroups";
HttpHeaders headers = new HttpHeaders();
headers.add("userId","38");
// headers.set("userId","38");
headers.setContentType(MediaType.APPLICATION_JSON);
Map
HttpEntity
ResponseEntity
String body = entity2.getBody();
return body;
}
使用Post方式,携带body请求数据
//注入
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/FaceInfo")
@ResponseBody
public Object faceInfo(String startTimerQCpdzNi,String endTime,Integer size ){
String apiURL = "http://0531yun.cn/wsjc/app/Login";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
Map
requestParam.put("loginName", "jnr");
requestParam.put("password", "jn");
HttpEntity
String s=request.toString();
ResponseEntity
String body = entity2.getBody();
return body;
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~