在日常接口开发中,通常涉及到与不同的终端进行交互,此时需要设计一个合适的接口协议体,以方便统一交互。接下来以在springboot中应用为例,来给出协议体定义案例。
返回协议体定义
这里给出的接口统一返回协议体涉及实体和MAP两种形式,下面直接贴上协议体的代码。
实体response
package com.lazycece.sbac.response.response;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* define the common response body
*
* @author lazycece
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResponseData<T> {
/**
* the response code
*/
private Integer status;
/**
* the response message
*/
private String message;
/**
* the response body
*/
private T body;
/**
* define success response data by default code and message, but no body
*
* @return response
*/
public static ResponseData success() {
return new ResponseData<>(ResponseCode.SUCCESS, ResponseMsg.SUCCESS, null);
}
/**
* define success response data by default code and message, the body is flexible
*
* @param body body
* @param <T> T
* @return response
*/
public static <T> ResponseData<T> success(T body) {
return new ResponseData<>(ResponseCode.SUCCESS, ResponseMsg.SUCCESS, body);
}
/**
* define the fail response by default code and message
*
* @return response
*/
public static ResponseData fail() {
return new ResponseData<>(ResponseCode.FAIL, ResponseMsg.FAIL, null);
}
/**
* define the fail response by default code, and the message is flexible
*
* @param message fail message
* @return response
*/
public static ResponseData fail(String message) {
return new ResponseData<>(ResponseCode.FAIL, message, null);
}
/**
* define the fail response, the code and message both flexible
*
* @param status code
* @param message message
* @return response
*/
public static ResponseData fail(Integer status, String message) {
return new ResponseData<>(status, message, null);
}
}
MAP形式response
package com.lazycece.sbac.response.response;
import java.util.HashMap;
/**
* @author lazycece
*/
public class ResponseMap extends HashMap<String, Object> {
private ResponseMap(Integer status, String message) {
this.put("status", status);
this.put("message", message);
}
/**
* define success response map on default code and message
*
* @return this
*/
public static ResponseMap success() {
return new ResponseMap(ResponseCode.SUCCESS, ResponseMsg.SUCCESS);
}
/**
* define fail response map on default code and message
*
* @return this
*/
public static ResponseMap fail() {
return new ResponseMap(ResponseCode.FAIL, ResponseMsg.FAIL);
}
/**
* define fail response map on default code
*
* @param message ""
* @return this
*/
public static ResponseMap fail(String message) {
return new ResponseMap(ResponseCode.FAIL, message);
}
/**
* define fail response map
*
* @param status ""
* @param message ""
* @return this
*/
public static ResponseMap fail(Integer status, String message) {
return new ResponseMap(status, message);
}
/**
* @param key key
* @param value value
* @return this
*/
public ResponseMap putting(String key, Object value) {
this.put(key, value);
return this;
}
}
message和code
public interface ResponseMsg {
String SUCCESS = "success";
String FAIL = "fail";
}
public interface ResponseCode {
Integer SUCCESS = 200;
Integer FAIL = 800;
}
使用案例
这里给出一个简单的使用案例:接口中直接返回一个用户实体。分别以两种返回协议体进行展现,这里只使用一个success方法。
下面直接贴上代码:
controller
package com.lazycece.sbac.response.controller;
import com.lazycece.sbac.response.entity.User;
import com.lazycece.sbac.response.response.ResponseData;
import com.lazycece.sbac.response.response.ResponseMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author lazycece
* @date 2019/02/22
*/
@RestController
@RequestMapping("/response")
public class ResponseController {
@GetMapping("/data/user/info")
public ResponseData getDataJson() {
return ResponseData.success(getUserInfo());
}
@GetMapping("/map/user/info")
public ResponseMap getMapJson() {
return ResponseMap.success().putting("data", getUserInfo());
}
private User getUserInfo() {
User user = new User();
user.setId("sfjsoop1jsjisf_1");
user.setUsername("lazycece");
user.setName("W");
user.setAge(200);
return user;
}
}
entity
package com.lazycece.sbac.response.entity;
import lombok.Data;
import java.util.Date;
/**
* @author lazycece
* @date 2019/02/22
*/
@Data
public class User {
private String id;
private String username;
private String name;
private Integer age;
private Date createTime = new Date();
private Date updateTime = new Date();
}
Junit test
package com.lazycece.sbac.response;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import javax.annotation.Resource;
/**
* @author lazycece
* @date 2019/02/22
*/
@RunWith(SpringRunner.class)
@WebMvcTest
public class ResponseControllerTest {
@Resource
private MockMvc mockMvc;
@Test
public void testGetDataJson() throws Exception {
MvcResult response = this.mockMvc
.perform(MockMvcRequestBuilders.get("/response/data/user/info"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
System.out.println(response.getResponse().getContentAsString());
}
@Test
public void testGetMapJson() throws Exception {
MvcResult response = this.mockMvc
.perform(MockMvcRequestBuilders.get("/response/map/user/info"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
System.out.println(response.getResponse().getContentAsString());
}
}
请求返回结果如下:
{"data":{"id":"sfjsoop1jsjisf_1","username":"lazycece","name":"W","age":200,"createTime":"2019-02-22T13:12:45.877+0000","updateTime":"2019-02-22T13:12:45.877+0000"},"message":"success","status":200}
案例源码
案例源码地址:https://github.com/lazycece/springboot-actual-combat/tree/master/springboot-ac-response