一篇文章带你掌握主流服务层框架——SpringMVC( 八 )

Postman操作:

一篇文章带你掌握主流服务层框架——SpringMVC

文章插图
这里我们简单介绍一下@DateTimeFormat的转换原理Converter接口:
public interface Converter<S,T>{ @NullableT convert(S var1)}Converter接口属于顶层接口 , 由它为起源创建了许多相关的接口与类用于各种转化:
  • 请求参数年龄数据(String->Integer)
  • 日期格式转发(String->Date)
@EnableWebMvc功能之一:根据类型匹配对应的类型转换器
设置响应在了解请求的相关知识之后 , 我们回到Controller代码中学习一下响应
跳转响应在正常情况下,我们的响应给出的是当前项目的文档,相当于页面的跳转效应:
package com.itheima.controller;import com.itheima.domain.User;import org.springframework.format.annotation.DateTimeFormat;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import java.util.ArrayList;import java.util.Date;import java.util.List;@Controllerpublic class UserController {//响应页面/跳转页面//返回值为String类型,设置返回值为页面名称,即可实现页面跳转@RequestMapping("/toJumpPage")public String toJumpPage(){System.out.println("跳转页面");return "page.jsp";}}信息响应如果我们希望得到一些信息响应,就需要采用注解解释:
package com.itheima.controller;import com.itheima.domain.User;import org.springframework.format.annotation.DateTimeFormat;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import java.util.ArrayList;import java.util.Date;import java.util.List;@Controllerpublic class UserController {//响应文本数据//返回值为String类型,设置返回值为任意字符串信息 , 即可实现返回指定字符串信息,需要依赖@ResponseBody注解@RequestMapping("/toText")@ResponseBodypublic String toText(){System.out.println("返回纯文本数据");return "response text";}//响应POJO对象//返回值为实体类对象,设置返回值为实体类类型 , 即可实现返回对应对象的json数据,需要依赖@ResponseBody注解和@EnableWebMvc注解@RequestMapping("/toJsonPOJO")@ResponseBodypublic User toJsonPOJO(){System.out.println("返回json对象数据");User user = new User();user.setName("itcast");user.setAge(15);return user;}//响应POJO集合对象//返回值为集合对象 , 设置返回值为集合类型,即可实现返回对应集合的json数组数据 , 需要依赖@ResponseBody注解和@EnableWebMvc注解@RequestMapping("/toJsonList")@ResponseBodypublic List<User> toJsonList(){System.out.println("返回json集合数据");User user1 = new User();user1.setName("传智播客");user1.setAge(15);User user2 = new User();user2.setName("黑马程序员");user2.setAge(12);List<User> userList = new ArrayList<User>();userList.add(user1);userList.add(user2);return userList;}}/*名称:@ResponseBody类型:方法注解位置:SpringMVC控制器方法定义上方作用:设置当前控制器返回值作为响应体*/当我们使用Postman访问该链接时就会给出对应反馈,这里就不做演示了
REST风格首先我们来简单介绍一下REST:
  • REST(Representational State Transfer) , 表现形式状态转换
我们给出正常风格和REST风格两种书写形式,我们可以明显看到REST的内容做出大规模的省略:
  • 正常风格:http://localhost/user/getById?id=1
  • REST风格:http://localhost/users/1
REST风格优点:
  • 书写简化
  • 隐藏资源的访问行为,无法通过地址得知对资源的操作
REST风格简介我们来对REST风格做出简单解释:
  • REST风格是采用访问资源的行为动作来区别对资源进行了何种操作
我们给出五种常见行为动作:
  • http://localhost/users:查看全部用户信息 GET(查询)
  • http://localhost/users/1: 查看指定用户信息 GET(查询)
  • http://localhost/users:添加用户信息 POST(新增/保存)
  • http://localhost/users:修改用户信息 PUT(修改/更新)
  • http://localhost/users/1: 删除用户信息 DELETE(删除)
我们通常将根据REST风格进行的访问称为RESTful
上述行为是约定方式,约定不是规范,是可以打破的,所以称为REST风格,而不是REST规范
描述模块的名称通常使用负数,也就是加s的格式描述,表示此类,而非单个资源
RESTful入门案例从本质上而言,REST只是一种规范形式,我们对于REST的风格修改仅针对于Controller

推荐阅读