三种方法实现调用Restful接口

1.HttpURLConnection实现

2.HttpClient实现

3.Spring的RestTemplate

一、HttpURLConnection实现

// HttpURLConnection 方式调用Restful接口
     // 调用接口
     @RequestMapping(value = "dealCon/{param}")
     public @ResponseBody String dealCon(@PathVariable String param) {
         try {
             String url = "http://localhost:8080/tao-manager-web/";
             url+=(param+"/xxx");
             URL restServiceURL = new URL(url);
             HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL
                     .openConnection();
             //param 输入小写,转换成 GET POST DELETE PUT 
             httpConnection.setRequestMethod(param.toUpperCase());
 //            httpConnection.setRequestProperty("Accept", "application/json");
             if("post".equals(param)){
                 //打开输出开关
                 httpConnection.setDoOutput(true);
 //                httpConnection.setDoInput(true);
                 
                 //传递参数
                 String input = "&id="+ URLEncoder.encode("abc", "UTF-8");
                 input+="&name="+ URLEncoder.encode("啊啊啊", "UTF-8");
                 OutputStream outputStream = httpConnection.getOutputStream();
                 outputStream.write(input.getBytes());
                 outputStream.flush();
             }
             if (httpConnection.getResponseCode() != 200) {
                 throw new RuntimeException(
                         "HTTP GET Request Failed with Error code : "
                                 + httpConnection.getResponseCode());
             }
             BufferedReader responseBuffer = new BufferedReader(
                     new InputStreamReader((httpConnection.getInputStream())));
             String output;
             System.out.println("Output from Server:  \n");
             while ((output = responseBuffer.readLine()) != null) {
                 System.out.println(output);
             }
             httpConnection.disconnect();
         } catch (MalformedURLException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }
         return "success";
     }

二、HttpClient实现 

 @RequestMapping(value = "dealCon2/{param}")
      public @ResponseBody User dealCon2(@PathVariable String param) {
          User user = null;
          try {
              HttpClient client = HttpClients.createDefault();
              if("get".equals(param)){
                  HttpGet request = new HttpGet("http://localhost:8080/tao-manager-web/get/"
                          +"啊啊啊");
                  request.setHeader("Accept", "application/json");
                  HttpResponse response = client.execute(request);
                  HttpEntity entity = response.getEntity();
                  ObjectMapper mapper = new ObjectMapper();
                  user = mapper.readValue(entity.getContent(), User.class);
              }else if("post".equals(param)){
                  HttpPost request2 = new HttpPost("http://localhost:8080/tao-manager-web/post/xxx");
                  List<NameValuePair> nvps = new ArrayList<NameValuePair>();  
                  nvps.add(new BasicNameValuePair("id", "啊啊啊"));  
                  nvps.add(new BasicNameValuePair("name", "secret"));
                  UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nvps, "GBK");
                 request2.setEntity(formEntity);
                 HttpResponse response2 = client.execute(request2);
                 HttpEntity entity = response2.getEntity();
                 ObjectMapper mapper = new ObjectMapper();
                 user = mapper.readValue(entity.getContent(), User.class);
             }else if("delete".equals(param)){
                 
             }else if("put".equals(param)){
                 
             }
         } catch (Exception e) {
             e.printStackTrace();
         }
         return user;
     }

三、Spring的RestTemplate

springmvc.xml增加

<!-- 配置RestTemplate -->
<!--Http client Factory -->
<bean id="httpClientFactory"
    class="org.springframework.http.client.SimpleClientHttpRequestFactory">
    <property name="connectTimeout" value="10000" />
    <property name="readTimeout" value="10000" />
</bean>  
<!--RestTemplate -->
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    <constructor-arg ref="httpClientFactory" />
</bean>

controller

@Controller
public class RestTemplateAction {
 
    @Autowired
     private RestTemplate template;
 
     @RequestMapping("RestTem")
     public @ResponseBody User RestTem(String method) {
         User user = null;
         //查找
         if ("get".equals(method)) {
             user = template.getForObject(
                     "http://localhost:8080/tao-manager-web/get/{id}",
                     User.class, "呜呜呜呜");
             
             //getForEntity与getForObject的区别是可以获取返回值和状态、头等信息
             ResponseEntity<User> re = template.
                     getForEntity("http://localhost:8080/tao-manager-web/get/{id}",
                     User.class, "呜呜呜呜");
             System.out.println(re.getStatusCode());
             System.out.println(re.getBody().getUsername());
             
         //新增
         } else if ("post".equals(method)) {
             HttpHeaders headers = new HttpHeaders();
             headers.add("X-Auth-Token", UUID.randomUUID().toString());
             MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<String, String>();
             postParameters.add("id", "啊啊啊");
             postParameters.add("name", "部版本");
             HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(
                     postParameters, headers);
             user = template.postForObject(
                     "http://localhost:8080/tao-manager-web/post/aaa", requestEntity,
                     User.class);
         //删除
         } else if ("delete".equals(method)) {
             template.delete("http://localhost:8080/tao-manager-web/delete/{id}","aaa");
         //修改
         } else if ("put".equals(method)) {
             template.put("http://localhost:8080/tao-manager-web/put/{id}",null,"bbb");
         }
         return user;
 
     }
}

上一篇:restful规范的认识?


下一篇:面试官:你们项目RESTful API设计规范是什么?