上篇文章已经介绍了SpringBoot 的基本使用过程,本文主要介绍用那篇文章中配置好的SpringBoot工程实现简单的接口。
最终工程目录是下面这样:
1. 工程目录结构
这是个简陋的目录结构,仅供我们参考,其中主要分为四类:
- repository
继承于JpaRepository,其中主要封装了操作数据库的sql动作,可以调其中的方法操作数据库,也可以自定义sql语句操作数据库。repository是Jpa的使者,是数据库的直接操作者; - controller
请求接口类、request/response类等,类前需要相关注解; - service
处理model实例,并将model实例通过repository同步给数据库; - model
数据实例,一个Entity对应数据库中的一个表。
最后还有一个..Application类,系统默认生成,不需要处理。
2. 各个类的实现
实现GET、POST类型的两个接口:
/todo/list
/todo/listAdd
- TodoController
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;
}
}
- service
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;
}
}
- 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; }
}
- repository
package com.dcxz.demo;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TodoRepository extends JpaRepository<Todo, Long> {
}
3. 调用接口
可以通过浏览器、PostMan调用以上写好的两个接口,来测试一下接口是否正常。在调用时注意检查IP地址、端口、接口名和参数是否设置正确。
网友评论