Tiven Wang
Wang Tiven July 25, 2017
425 favorite favorites
bookmark bookmark
share share

Oryx on top of a dune in the NamibRand Nature Reserve, Namibia.

下載本文完整項目代碼 Github

本文在 Spring Boot 構建項目基礎上進行的,如果讀者想要了解 Spring Boot 項目如何創建請參考Try Cloud Foundry 7 - Spring Boot

Spring Web

Setup Spring Web

如果你的Spring Boot項目裡還沒有添加Spring Web依賴,可以添加如下組件:


Build Restful API

使用 @RestController @RequestMapping 可以编写我们的 Restful API。

public class HeroController {

  @Autowired(required = false) HeroRepository heroRepository;

  Iterable<Hero> getAll() {
      return heroRepository.findAll();

  @RequestMapping(path="", method=RequestMethod.POST)
  Hero create(@RequestBody Hero hero) {
      return heroRepository.save(hero);

  Hero get(@PathVariable BigInteger id) {
      return heroRepository.findOne(id);

  @RequestMapping(path="/{id}", method=RequestMethod.DELETE)
  void delete(@PathVariable BigInteger id) {


Spring Data Commons

如果你的項目使用了 Spring Data framework 的話,參考文檔Spring Data Commons - Reference Documentation - 3.8.2. Web support 配置對 Web 的支持。


class WebConfiguration { }

@EnableSpringDataWebSupport 註解會啟動對Restful API的特性的支持。

如果使用了 Spring Boot @EnableAutoConfiguration 自動發現配置功能(@SpringBootApplication包含了@EnableAutoConfiguration),則不需要@EnableSpringDataWebSupport,Spring Boot會自動查找到 SpringDataWebConfiguration 並進行正確的配置。


<bean class="org.springframework.data.web.config.SpringDataWebConfiguration" />

<!-- If you're using Spring HATEOAS as well register this one *instead* of the former -->
<bean class="org.springframework.data.web.config.HateoasAwareSpringDataWebConfiguration" />

以上方式中的 Spring HATEOAS 都是在你把 Spring HATEOAS 組件加入項目依賴中自動發現配置的。

Paging and Sorting


public interface HeroRepository extends PagingAndSortingRepository<Hero, BigInteger> {

然後 RestController 的 query method 加上參數 Pageable pageable:

Page<Hero> getAll(Pageable pageable) {
    return heroRepository.findAll(pageable);


Spring Data Rest

Spring 提供了更为简便的方式创建 Restful APIs , Spring Data REST is part of the umbrella Spring Data project and makes it easy to build hypermedia-driven REST web services on top of Spring Data repositories.


Spring data rest configuration with Maven:


為 Hero Repository 添加註解 @RepositoryRestResource(collectionResourceRel = "heros", path = "heros"),為了與@RestController說明的 Restful api 區分這裡使用heros作為路徑。

@RepositoryRestResource(collectionResourceRel = "heros", path = "/heros")
public interface HeroRepository  extends CrudRepository<Hero, BigInteger> {

啟動應用 mvn spring-boot:run 則可以看到 hero 的 restful apis :

Spring Data REST uses the HAL format for JSON output. It is flexible and offers a convenient way to supply links adjacent to the data that is served.


你的 HeroRespository 也可以继承 PagingAndSortingRepository 或者 MongoRepository之类的,可以提供更为丰富的 Restful 功能。

Similar Posts

  • Microservices - API Gateway : Zuul .
  • Microservices - Transactions
  • Microservices - API Gateway Implement an API gateway that is the single entry point for all clients. The API gateway handles requests in one of two ways. Some requests are simply proxied/routed to the appropriate service. It handles other requests by fanning out to multiple services.
  • Microservices - Circuit Breaker 访问远程服务时, 比依赖超时时间更好一些的方式是一种叫断路器(Circuit Breaker)的模式. Circuit Breaker 就像一位交通警察, 在前方道路畅通的情况下, 他会放行; 当前方道路由于各种原因拥堵时, 他会告诉你前方道路不通请回; 如果他是个更智能的交警的话, 还会告诉你前方道路部分拥堵, 只允许部分车辆通过, 比如实行单双号.
  • Microservices - Architecture Microservice architecture (MSA) is an approach to building software systems that decomposes business domain models into smaller, consistent, bounded-contexts implemented by services. These services are isolated and autonomous yet communicate to provide some piece of business functionality. Microservices are typically implemented and operated by small teams with enough autonomy that each team and service can change its internal implementation details (including replacing it outright!) with minimal impact across the rest of the system.
  • Microservices - Load Balancing 在微服务架构中每个服务都可能有多个运行实例, 那么服务消费者在调用服务时怎么定位应该把请求发送给哪个实例呐? 这就是负载均衡 Load Balancing 要做的事情。Load Balancing 可以在服务端也可以是在消费端。


Back to Top