栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

前言

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


文章目录
  • 前言
  • 一、Restful Web Service
    • 1.1 Restful Web Service简述
    • 1.2 Rest设计规范
  • 二、基于通用Mapper基于Rest编写api接口
    • 2.1 定义Mapper接口
    • 2.2 定义mapper.xml文件
    • 2.3 定义pojo类
    • 2.4 定义Service接口
    • 2.5 定义Service实现类Impl
    • 2.5 定义Controller
  • 三、使用PostMan调试Restful接口
    • 3.1 PostMan简介
  • 总结


一、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 {
}
2.2 定义mapper.xml文件



  
    
    
    
    
  

2.3 定义pojo类
import javax.persistence.Id;

public class Student {
    @Id
    private Integer id;

    private String name;

    private Integer age;

    
    public Integer getId() {
        return id;
    }

    
    public void setId(Integer id) {
        this.id = id;
    }

    
    public String getName() {
        return name;
    }

    
    public void setName(String name) {
        this.name = name;
    }

    
    public Integer getAge() {
        return 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);
    }

    
    @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/

总结

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/953140.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号