Java架构师—关于Restful Web Service的那些事儿

前言

       Restful Web Service简述;基于通用Mapper基于Rest编写api接口;使用PostMan调试Restful接口。




一、Restful Web Service

1.1 Restful Web Service简述

  • 通信方式
  • 信息传递
  • 无状态
  • 独立性

1.2 Rest设计规范

GET -> /order/{id} -> /getOrder?id=1001
POST -> /order -> /saveOrder
PUT -> /order/{id} -> /modifyOrder
DELETE -> /order/{id} -> /deleteOrder?id=1001

二、基于通用Mapper基于Rest编写api接口

2.1 定义Mapper接口

import com.imooc.my.mapper.MyMapper;
import com.imooc.pojo.Student;

public interface StudentMapper extends MyMapper<Student> {
}

2.2 定义mapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.imooc.mapper.StudentMapper" >
  <resultMap id="BaseResultMap" type="com.imooc.pojo.Student" >
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="age" property="age" jdbcType="INTEGER" />
  </resultMap>
</mapper>

2.3 定义pojo类

import javax.persistence.Id;

public class Student {
    @Id
    private Integer id;

    private String name;

    private Integer age;

    /**
     * @return id
     */
    public Integer getId() {
        return id;
    }

    /**
     * @param id
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return age
     */
    public Integer getAge() {
        return age;
    }

    /**
     * @param age
     */
    public void setAge(Integer age) {
        this.age = age;
    }
}

2.4 定义Service接口

import com.imooc.pojo.Student;

public interface StudentService {

    public Student getStudentIndo(int id);

    public void saveStudent();

    public void updateStudent(int id);

    public void deleteStudent(int id);
}

2.5 定义Service实现类Impl

import com.imooc.mapper.StudentMapper;
import com.imooc.pojo.Student;
import com.imooc.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class StudentServiceImpl implements StudentService {

    // 开发工具问题:Settings->Editor->Code Style->inspections->Spring->Spring Core->Core->Autowiring for Bean Class 取消勾选
    @Autowired
    private StudentMapper studentMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Student getStudentIndo(int id) {
        return studentMapper.selectByPrimaryKey(id);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void saveStudent() {
        Student student = new Student();
        student.setName("DONGJ");
        student.setAge(16);
        studentMapper.insert(student);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateStudent(int id) {
        Student student = new Student();
        student.setId(id);
        student.setName("DONGJ");
        student.setAge(18);
        studentMapper.updateByPrimaryKey(student);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void deleteStudent(int id) {
        studentMapper.deleteByPrimaryKey(id);
    }
}

2.5 定义Controller

import com.imooc.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

//@Controller
@ApiIgnore
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/getStudent")
    public Object getStudent(int id) {
        return studentService.getStudentIndo(id);
    }

    /**
     * TODO 接口幂等性
     * @return
     */
    @PostMapping("/saveStudent")
    public Object saveStudent() {
        studentService.saveStudent();
        return "OK";
    }

    @PostMapping("/updateStudent")
    public Object updateStudent(int id) {
        studentService.updateStudent(id);
        return "OK";
    }

    @PostMapping("/deleteStudent")
    public Object saveStudent(int id) {
        studentService.deleteStudent(id);
        return "OK";
    }
}

三、使用PostMan调试Restful接口

3.1 PostMan简介

       Postman 是一种常用的接口测试工具,可以发送几乎所有类型的HTTP请求。Postman适用于不同的操作系统,Postman Mac、Windows X32、Windows X64、Linux系统,还支持postman 浏览器扩展程序、postman chrome应用程序等。官网地址:https://www.postman.com/
PostMan

总结

Restful Web Service简述;基于通用Mapper基于Rest编写api接口;使用PostMan调试Restful接口。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Secure your RESTful applications against common vulnerabilities Overview Learn how to use, configure, and set up tools for applications that use RESTful web services to prevent misuse of resources Get to know and fix the most common vulnerabilities of RESTful web services APIs A step-by-step guide portraying the importance of securing a RESTful web service with simple examples applied to real-world scenarios In Detail This book will serve as a practical companion for you to learn about common vulnerabilities when using RESTful services, and will provide you with an indispensable knowledge of the tools you can use to implement and test security on your applications. It will cover the fine details of setting up RESTful services such as implementing RESTEasy and securing transmission protocols such as the OAuth protocol and its integration with RESTEasy. Furthermore, it also explains the implementation of digital signatures and the integration of the Doseta framework with RESTEasy. With this book, you will be able to design your own security implementation or use a protocol to grant permissions over your RESTful applications with OAuth. You will also gain knowledge about the working of other features such as configuring and verifying HTTP and HTTPS protocols, certificates, and securing protocols for data transmission. By the end of this book, you will have comprehensive knowledge that will help you to detect and solve vulnerabilities. What you will learn from this book Set up, implement, and personalize your development and test environment Learn, understand, and assimilate concepts inherent to security management on RESTful applications and the importance of these concepts Implement and test security on your applications that use RESTful web services with the most useful techniques and interpret the test results Apply and configure secure protocols on your application Implement, configure, and integrate other technologies such as OAuth or SSO with RESTful applications Learn and assimilate security concepts at JEE application and container level Understand digital signatures and message encryption through descriptive examples Approach A sequential and easy-to-follow guide which allows you to understand the concepts related to securing web apps/services quickly and efficiently, since each topic is explained and described with the help of an example and in a step-by-step manner, helping you to easily implement the examples in your own projects. Who this book is written for This book is intended for web application developers who use RESTful web services to power their websites. Prior knowledge of RESTful is not mandatory, but would be advisable. Table of Contents Chapter 1. Setting Up the Environment Chapter 2. The Importance of Securing Web Services Chapter 3. Security Management with RESTEasy Chapter 4. RESTEasy Skeleton Key Chapter 5. Digital Signatures and Encryption of Messages

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值