pom 引入 commons-httpclientjava
HttpClient client = new HttpClient(); List<Header> headers = new ArrayList<Header>(); headers.add(new Header("SOMETIME","someContent")); headers.add(new Header("API-REMOTEIP", "http://10.1.41.252")); client.getHostConfiguration().getParams().setParameter("http.default-headers", headers); // 使用 GET 方法 ,若是服務器須要經過 HTTPS 鏈接,那隻須要將下面 URL 中的 http 換成 https HttpMethod method=new GetMethod("http://localhost:8080/api/category/1"); //使用POST方法 //HttpMethod method = new PostMethod("http://java.sun.com"); client.executeMethod(method); //打印服務器返回的狀態 System.out.println(method.getStatusLine()); //打印返回的信息 System.out.println(method.getResponseBodyAsString()); //釋放鏈接 method.releaseConnection();
@RequestHeader(value="SOMETIME") String sometime, @RequestHeader(value="API-REMOTEIP") String apiRemoteip
拿到定製Header就能夠作本身的業務了,好比灰度系統等。api