在Spring Cloud集群中,各个角色的通信基于REST服务,因此在调用服务时,就不可避免地需要使用REST服务器请求的客户端。我们知道Spring可以用自带的RestTemplate使用HttpClient发送请求,此处将介绍另一个REST客户端:Feign。Feign框架已经被集成到Spring Cloud的Netflix项目中,使用该框架可以在Spring Cloud的集群中更加简单地调用REST服务。

  在学习Feign前,我们先了解一下REST客户端。本文将简单地讲述Apache CXF与Restlet这两款Web Service框架。并使用者两个框架来编写REST客户端,最后在编写一个Feign的Hello World例子。通过此过程,我们可以对Feign有一个初步的印象。

  创建3个maven项目,用于本节内容的测试,目录结构如下

07 REST客户端

  创建一个maven项目并引入依赖

  pom.xml代码清单

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.triheart</groupId>
    <artifactId>restserver</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.4.RELEASE</version>
        </dependency>
        <!--
            服务器端要加上这个依赖,否则客户端在请求时会报以下异常:
            Exception in thread "main" feign.FeignException: status 415 reading PersonClient#createPersonXML(Person); content:
            {"timestamp":1502705981406,"status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content type 'application/xml;charset=UTF-8' not supported","path":"/person/createXML"}

         -->
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-xml-provider</artifactId>
            <version>2.9.0</version>
        </dependency>
    </dependencies>
    
</project>

  编写启动类以及控制器发布服务

  启动类代码清单RestServerApp.java

package com.triheart.restserver;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

/**
 * @author 阿遠
 * Date: 2018/8/28
 * Time: 17:07
 */
@SpringBootApplication
public class RestServerApp {

    public static void main(String[] args) {
        new SpringApplicationBuilder(RestServerApp.class).run(args);
    }
}

  控制器代码清单

  MyController.java

package com.triheart.restserver;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 阿遠
 * Date: 2018/8/28
 * Time: 17:10
 */
@RestController
    public class MyController {

    /**
     * 查询方法,参数为Person的id
     */
    @RequestMapping(value = "/person/{personId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Person findPerson(@PathVariable("personId") Integer personId,
                             HttpServletRequest request) {
        Person p = new Person();
        p.setId(personId);
        p.setName("Crazyit");
        p.setAge(30);
        p.setMessage(request.getRequestURL().toString());
        return p;
    }

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() {
        return "Hello World";
    }   
} 

  Person.java

package com.triheart.restserver;

/**
 * @author 阿遠
 * Date: 2018/8/28
 * Time: 17:09
 */
public class Person {

    private Integer id;

    private String name;

    private Integer age;

    private String message;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
View Code

相关文章:

  • 2021-12-06
  • 2022-12-23
  • 2021-10-03
  • 2021-11-29
  • 2021-12-31
  • 2021-07-04
  • 2021-08-15
  • 2022-12-23
猜你喜欢
  • 2021-04-01
  • 2022-12-23
  • 2021-12-18
  • 2021-05-24
  • 2021-06-03
  • 2022-12-23
  • 2021-12-23
相关资源
相似解决方案