zoukankan      html  css  js  c++  java
  • springboot Restful开发

    一,REST的概念及原则:

      1,REST(Representational State Transfer,表述性状态转移),定义了互联网软件的架构原则,是一种面向资源的架构风格。互联网中,客户端和服务端之间的互动传递的就只是资源的表述。我们上网的过程,就是调用资源的URI,获取它不同表现形式的过程。这种互动只能使用无状态协议HTTP,也就是说,服务端必须保存所有的状态,客户端可以使用HTTP的几个基本操作,使得服务端上的资源发生“状态转换”。

      2,原则:            

    • 网络上的所有事务都被抽象为资源
    • 每个资源都有一个唯一的资源标识符
    • 同一个资源具有多种表现形式(xml,json等)
    • 对资源的各种操作不会改变资源标识符
    • 所有的操作都是无状态的

      3,使用情况举例:

    ------->Spring Boot全面支持开发RESTful程序,通过不同的注解来支持前端的请求:

    •   @GetMapping,处理Get请求
    •   @PostMapping,处理Post请求
    •   @PutMapping,用于更新资源
    •   @DeleteMappping,处理删除请求
    •   @PatchMapping,用于更新部分资源

    ------->Controller中的映射注解

      @PathVariable,用于接收url路径上的参数

      @ModelAttribute,用于直接接受url?后面的参数 ,如url?id=123&name=456,然后直接转为POJO

    二,Springboot下进行RESTful开发。

    (一)在POM文件中引入Jpa,mysql,devtoolks依赖,并在build标签里开启热部署:

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency> 
        <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>

    </dependency>

    (二),配置Properties,数据库环境及JPA

     (三),配置idea的database连接数据库;注意需在下图框中引入具体jar包。(mysql5.0之后,驱动类变为com.mysql.cj.jdbc.Driver,且需要在url后加入?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true

     (四)书写repository,里面不用自定义方法。继承JpaRepository<UsersEntity,Integer>类即可。(之后会加关于此类的详细介绍)

    (五)书写controller,完成查找,根据id查找,新增,修改,删除等方法的构建

    @RestController
    public class UsersController {
        @Autowired
        private UserRepository userRepository;
    ///查询所有数据
        @RequestMapping(value="users",method= RequestMethod.GET)
        public List<UsersEntity> findAll(){
            return userRepository.findAll();
        }
    //根据id查询对象
        @RequestMapping(value="users/{id}",method=RequestMethod.GET)
        public UsersEntity findById(@PathVariable int id){
            return userRepository.findById(id).get();
        }
    //保存对象
        @RequestMapping(value = "users",method=RequestMethod.POST)
        public UsersEntity saveUsers(@ModelAttribute UsersEntity users){
            return userRepository.save(users);
        }
        //修改对象
        @RequestMapping(value="users",method = RequestMethod.PUT)
        public UsersEntity updateUsers(@ModelAttribute UsersEntity users){
            return userRepository.saveAndFlush(users);
        }
    
        @RequestMapping(value="users/{id}",method = RequestMethod.DELETE)
        public  void deleteById(@PathVariable int id){
            userRepository.deleteById(id);
    
        }
    }

    三,进行Spring Data Rest的开发。

      Spring Data Rest作为Spring Data项目的子集,开发者只需使用注解@RepositoryRestResource标记,就可以把整个Repository转换为HAL风格的REST资源,目前已支持Spring Data JPA,Spring DataMongoDB,Spring Data Neo4j等等。简单点说,Spring Data Rest把我们需要编写的大量REST模板接口做了自动化实现。并符合HAL的规范。

    官方文档:https://www.springcloud.cc/spring-data-rest-zhcn.html

      

  • 相关阅读:
    Sql Server 2008卸载后再次安装一直报错
    listbox 报错 Cannot have multiple items selected when the SelectionMode is Single.
    Sql Server 2008修改Sa密码
    学习正则表达式
    Sql Server 查询第30条数据到第40条记录数
    Sql Server 复制表
    Sql 常见面试题
    Sql Server 简单查询 异步服务器更新语句
    jQuery stop()用法以及案例展示
    CSS3打造不断旋转的CD封面
  • 原文地址:https://www.cnblogs.com/boogie-xy/p/12838610.html
Copyright © 2011-2022 走看看