src\main\java\com\dev1\entity\Rest.java
package com.dev1.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Data
public class Rest {
private Integer code;
private String msg;
private Object data;
}
src\main\java\com\dev1\controller\AccountController.java
package com.dev1.controller;
import com.dev1.entity.Account;
import com.dev1.entity.Rest;
import com.dev1.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController //作用:所有方法的返回转成json
@RequestMapping("/account") //模块
public class AccountController {
@Autowired
IAccountService accountService;
@RequestMapping(value = "/add",method = RequestMethod.POST)
public Rest add(@RequestBody Account account){ //将json转成对象
accountService.addAccount(account);
Rest rest = new Rest(200,"添加成功",null);
return rest;
}
@RequestMapping(value = "/update",method = RequestMethod.POST)
public Rest update(@RequestBody Account account){ //将json转成对象
accountService.updateAccount(account);
Rest rest = new Rest(200,"修改成功",null);
return rest;
}
@RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
public Rest delete(@PathVariable("id") String id){
System.out.println("--delete--"+id);
accountService.deleteAccountById(id);
Rest rest = new Rest(200,"删除成功",null);
return rest;
}
@RequestMapping(value = "/find/{id}",method = RequestMethod.GET)
public Rest findById(@PathVariable("id") String id){
System.out.println("--findById--"+id);
Account account = accountService.findAccountById(id);
Rest rest = new Rest(200,"查询成功",account);
return rest;
}
@RequestMapping(value = "/find",method = RequestMethod.GET)
public Rest findAll(){
List<Account> list = accountService.findAccounts();
Rest rest = new Rest(200,"查询成功",list);
return rest;
}
}
测试
编写API接口文档
1.Account模块Restful
1.1.添加
/account/add
提交
{
"id":"104",
"name": "jack2",
"value": 900
}
返回
{
"code": 200,
"msg": "添加成功",
"data": null
}
1.2.修改
/account/update
提交
{
"id":"104",
"name": "tony",
"value": 9000
}
{
"code": 200,
"msg": "修改成功",
"data": null
}
1.3.删除
/account/delete/101
提交
无
{
"code": 200,
"msg": "删除成功",
"data": null
}
1.4.查一个
/account/find/101
提交
无
{
"code": 200,
"msg": "查询成功",
"data": {
"id": "101",
"name": "jack",
"value": 100
}
}
1.5.查所有
/account/find
提交
无
{
"code": 200,
"msg": "查询成功",
"data": [
{
"id": "101",
"name": "jack",
"value": 100
},
{
"id": "102",
"name": "rose",
"value": 100
},
{
"id": "103",
"name": "tony",
"value": 100
}
]
}