Coder Social home page Coder Social logo

simpleboot's Introduction

simpleboot

介绍

学习 javaGuide,动手造个轮子。 simpleboot 是一个 模仿 Spring Boot 写的一个轻量级的 HTTP 框架。

Get请求和POST请求处理

  • @GetMapping : 处理Get请求
  • @PostMapping :处理 Post 请求
  • @RequestBody : 接收前端传递给后端的json字符串
  • @RequestParam :获取Get请求的 URL 查询参数
  • @PathVariable : 获取 URL 中的参数/占位符

IOC

  • @Autowired :注入对象
  • @Component :声明对象被 IOC容器管理

异常处理

  • @ControllerAdvice :
  • @ExceptionHandler :

代码质量

  • 集成 checkstyle
  • 集成 spotbugs

功能演示

User.java :用户实体类

@Data
@AllArgsConstructor
public class User {
    private String name;
    private String des;
    private Integer age;
}

UserDto.java : 创建用户的传输对象

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserDto {
    private String name;
    private String des;
    private Integer age;
}

UserController.java:用户层controller

@RestController("/user")
public class UserController {

    private static Map<Integer, User> users;
    private static Integer id;

    {
        users = new HashMap<>();
        users.put(1, new User("Lue", "SH", 22));
        id = 2;
    }
    @GetMapping
    public User get(@RequestParam("name") String name, @RequestParam("age") Integer age) {
        System.out.println(name);
        return new User(name, "EMM", age);
    }

    @GetMapping("/{id}")
    public User get(@PathVariable("id") Integer id) {

        return users.get(id);
    }

    @PostMapping
    public List<User> create(@RequestBody UserDto userDto) {
        users.put(id,new User(userDto.getName(), userDto.getDes(), userDto.getAge()));
        return new ArrayList<>(users.values());
    }

}

Get 请求:@RequestParam 传参

Get 请求:@PathVariable 传参

Post 请求:@RequestBody 传参

simpleboot's People

Contributors

lmgty avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.