在虚拟机的docker中装了一个httpbin,可以用来进行接口测试,ip地址是192.168.10.101。
使用的是restTemplate。
RestTemple是Spring提供的用于访问Http请求的客户端,RestTemple提供了多种简洁的远程访问服务的方法
restTemplate.exchange可以对给定的URI模板执行HTTP方法,将给定的请求实体写入请求,并将响应作为ReponseEntity返回.
@PostMapping ("/submit") public String submit( HttpServletRequest request ) throws IOException { Enumeration< String > parameterNames = request.getParameterNames(); request.getHeader( "authorization" ); System.out.println("come in submit func"); ObjectMapper objectMapper = new ObjectMapper(); String authorization = request.getHeader( "authorization" ); System.out.println(authorization); String url = "http://192.168.10.101/ip"; HttpHeaders headers = new HttpHeaders(); headers.setContentType( MediaType.APPLICATION_JSON); HttpEntity entity = new HttpEntity( null,headers ); HttpMethod methodType = HttpMethod.GET; ResponseEntity< String > exchange = restTemplate.exchange( url, methodType, entity, String.class ); Origin origin = objectMapper.readValue( exchange.getBody().toString(), Origin.class ); System.out.println(origin); return origin.getOrigin(); }
对httpbin发送一个获取id的方法。