通过org.springframework.hateoas.ResourceSupport来处理超媒体部分数据结构。

假设要提供的超媒体数据结构如下:

{"content": "Hello, World!","_links": {"self": {"href": "http://localhost:8080/greeting?name=World"}}
}

下面是提供Hypermedia-Driven RESTful Web Service程序的主要代码:
1 model类

package hypermedia.restfull.web.service.hello;import org.springframework.hateoas.ResourceSupport;import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;public class Greeting extends ResourceSupport {private final String content;@JsonCreatorpublic Greeting(@JsonProperty("content")String content){this.content = content;}public String getContent() {return content;}}

说明:

To model the greeting representation, you create a resource representation class. As the _links property is a fundamental property of the representation model, Spring HATEOAS ships with a base class ResourceSupport that allows you to add instances of Link and ensures that they are rendered as shown above.

2 controller类

package hypermedia.restfull.web.service.hello;import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class GreetingController {private static final String TEMPLATE = "Hello, %s!";@RequestMapping("/greeting")public HttpEntity<Greeting> greeting(@RequestParam(value="name", required=false, defaultValue="World")String name){Greeting greeting = new Greeting(String.format(TEMPLATE, name));greeting.add(linkTo(methodOn(GreetingController.class).greeting(name)).withSelfRel());return new ResponseEntity<Greeting>(greeting, HttpStatus.OK);}
}

说明:

Because the @RestController annotation is present on the class, an implicit @ResponseBody annotation is being added onto the greeting method. This causes Spring MVC to render the returned HttpEntity and its payload, the Greeting, directly to the response.

Both linkTo(…) and methodOn(…) are static methods on ControllerLinkBuilder that allow you to fake a method invocation on the controller. The LinkBuilder returned will have inspected the controller method’s mapping annotation to build up exactly the URI the method is mapped to.

3 application类

package hypermedia.restfull.web.service.hello;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}

SpringBoot之Hypermedia-Driven RESTful Web Service相关推荐

  1. Java RESTful Web Service实战(第2版)

    Java核心技术系列 Java RESTful Web Service实战 (第2版) 韩陆 著 图书在版编目(CIP)数据 Java RESTful Web Service实战 / 韩陆著. -2版 ...

  2. RESTful Web Service 架构剖析

    如今微服务和分布式架构变的越来越流行,而简单,可靠,高效,跨平台和跨语言的 Web Service 则是这类系统架构的基石. RESTful Web Service 恰好满足这些特点,被越来越多的系统 ...

  3. WCF实现RESTFul Web Service

    共同学习了前面一些概念,终于开始正题了哈.RESTful的Web Service调用直观,返回的内容容易解析.这里先会描述一个简单的场景--Web Service提供一个方法来搜索个人信息,传入人名, ...

  4. Java RESTful Web Service实战

    编者按: InfoQ开设栏目"品味书香",精选技术书籍的精彩章节,以及分享看完书留下的思考和收获,欢迎大家关注.本文节选自韩陆著<Java RESTful Web Servi ...

  5. 【转】Spring 4.x实现Restful web service

    http://my.oschina.net/yuyidi/blog/352909 首先我们还是跟之前一样,创建一个maven项目,不过因为Spring Restful web service是基于Sp ...

  6. 使用JAX-RS创建RESTful Web Service

    guice resteasy http://www.cnblogs.com/ydxblog/p/7891224.html http://blog.csdn.net/withiter/article/d ...

  7. JAX-RS 方式的 RESTful Web Service 开发

    2019独角兽企业重金招聘Python工程师标准>>> Web Service 目前在风格上有两大类,一个是基于 SOAP 协议,一个是完全遵循 HTTP 协议规范的RESTful  ...

  8. spring3创建RESTFul Web Service

    spring 3支持创建RESTFul Web Service,使用起来非常简单.不外乎一个@ResponseBody的问题. 例如:后台controller: 做一个JSP页面,使用ajax获取数据 ...

  9. Apache CXF实现Web Service(3)——Tomcat容器和不借助Spring的普通Servlet实现JAX-RS(RESTful) web service...

    起步 参照这一系列的另外一篇文章: Apache CXF实现Web Service(2)--不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service 首先 ...

  10. 关于Restful Web Service的一些理解

    对于restful web service,刚开始确实是个不太好理解的概念. 看了些文章,总是在描述和解释restful的原则: 1)显式地使用HTTP方法. 2)无状态. 3)公开目录结构式的 UR ...

最新文章

  1. vs快捷键及常用设置(vs2012版)
  2. 元气满满 开工大吉 2020「升职加薪,走好这三步...」
  3. zset中的score_每天五分钟,成就redis大神之Zset数据类型
  4. 追源索骥:透过源码看懂Flink核心框架的执行流程
  5. BUAA - 各项目组对软件用户数的估计
  6. android按钮置于顶层,如何把按键显示在最顶层窗口上(屏幕最顶上)
  7. java ntpudpclient_使用NTP获取网络时间-----java
  8. Winform 初识记录
  9. html表单提交数据保存到mysql中_php – Canonical:如何将HTML表单数据保存到MySQL数据库中...
  10. 行业方案|“医疗”行业智能运维解决方案介绍
  11. STM32LCD显示汉字
  12. Validation and Data leakages
  13. 需要将录音转文字在线转换,应该如何完成
  14. ChatGPT 团队有几个人?
  15. 上市公司融资需要哪些条件
  16. 【原型模式】原型模式深入分析
  17. nginx proxy_pass规则
  18. raid卡超级电容和电池的区别
  19. Naxx Central District 1 Ice dragon saffron
  20. 图像特征提取——灰度共生矩阵(GLCM)

热门文章

  1. 软件工程(二)——过程模型
  2. 数学建模 之 matlab初学两天搞定基础 极限求导积分篇(5/5)
  3. SQLite3 学习3
  4. 电商购物评论的情感分析
  5. Win10触摸板手势及技巧
  6. Unterminated regular expression 和正则表达式
  7. 知识兔课程揭秘2021抖音卖货代运营的新骗局,你中招了吗?
  8. 新西兰梅西大学计算机专业研究生,新西兰梅西大学硕士读几年,新西兰专升硕 | 能进八大,还有机会移民,最快1.5年完成!...
  9. 芯片的原理应用与分类
  10. Canto助力金融衍生品市场持续、健康、快速发展