Python 官方文档:入门教程 => 点击学习
目录Http如何调用其他服务1.GET请求2.POST请求SpringBoot请求其他服务器的http接口使用Get方式,携带headers请求数据使用Post方式,携带body请求
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.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 测试成功!";
}
}
//注入
@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;
}
//注入
@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;
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: Springboot HTTP如何调用其他服务
本文链接: https://lsjlt.com/news/164098.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0