云 & 网站

用 SpringBoot 实现简单的接口

2020-04-24  本文已影响0人  大成小栈

上篇文章已经介绍了SpringBoot 的基本使用过程,本文主要介绍用那篇文章中配置好的SpringBoot工程实现简单的接口。

最终工程目录是下面这样:

1. 工程目录结构

这是个简陋的目录结构,仅供我们参考,其中主要分为四类:

最后还有一个..Application类,系统默认生成,不需要处理。

2. 各个类的实现

实现GET、POST类型的两个接口:
/todo/list
/todo/listAdd

package com.dcxz.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
public class TodoController {

    @Autowired
    TodoService todoService;
    
    @RequestMapping(value = "/todo/list", method = RequestMethod.GET)
    public TodoResponse getTodoList(@RequestParam long id) {

        Todo todo = todoService.getTodoData(id);

        TodoResponse response = new TodoResponse();
        response.setName(todo.getName());
        //response.setMember(100);
        //response.setCount(20);
        return response;
    }


    @RequestMapping(value = "/todo/listAdd", method = RequestMethod.POST)
    public String addTodoList(@RequestBody AddTodoRequest request) {

        String name = request.getName();
        if (name == null) { throw new RuntimeException("参数错误"); }

        todoService.addTodoData(name);

        return "success!";
    }
}
/// AddTodoRequest
package com.dcxz.demo;

public class AddTodoRequest {

    private String name;

    public String getName() {
        return name;
    }

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


/// TodoResponse
package com.dcxz.demo;

import java.util.Set;

public class TodoResponse {

    private String name;
    private int count;
    private int member;

    public String getName() {
        return name;
    }

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

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getMember() {
        return member;
    }

    public void setMember(int member) {
        this.member = member;
    }
}
package com.dcxz.demo;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;

@Service
public class TodoService {

    @Autowired
    TodoRepository todoRepository;

    public void addTodoData(String name) {

        Todo todo = new Todo();
        todo.setName(name);
        todo.setCreateTime(new Date());

        todoRepository.save(todo);
    }


    public Todo getTodoData(long id) {

        Todo todo = todoRepository.findById(id).get();
        return todo;
    }
}
package com.dcxz.demo;


import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "s_todo")
public class Todo {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    private String name;

    @Column(name = "create_time")
    private Date createTime;


    public long getId() { return id; }
    public void setId(long id) { this.id = id; }

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

    public Date getCreateTime() { return createTime; }
    public void setCreateTime(Date createTime) { this.createTime = createTime; }
}
package com.dcxz.demo;

import org.springframework.data.jpa.repository.JpaRepository;

public interface TodoRepository extends JpaRepository<Todo, Long> {
}

3. 调用接口

可以通过浏览器、PostMan调用以上写好的两个接口,来测试一下接口是否正常。在调用时注意检查IP地址、端口、接口名和参数是否设置正确。

上一篇下一篇

猜你喜欢

热点阅读