筆者的web項目中使用RESTFul規範和前台進行互動。
原始代碼
傳回的json資料格式如下:
對應的背景實體類及互動方法:
JsonResult.java
public class JsonResult {
private int code;
private String message;
private String nextUrl;
private Object data;
public JsonResult(int code, String message) {
this.code = code;
this.message = message;
}
public JsonResult(int code, String message, Object data) {
this.code = code;
this.message = message;
this.data = data;
}
public JsonResult(int code, String message, String nextUrl) {
this.code = code;
this.message = message;
this.nextUrl = nextUrl;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getNextUrl() {
return nextUrl;
}
public void setNextUrl(String nextUrl) {
this.nextUrl = nextUrl;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
controller代碼:
@PostMapping(value = "offline")
@ResponseBody
public JsonResult offline() {
if(xxxxx)
return errorResult("appid無效");
if(yyy){
ImmutableMap<String, Object> result = ImmutableMap.of("uuid", conversionId, "code", 200);
return successResult("轉換成功", result);
}
}
protected JsonResult successResult(String message, String nextUrl) {
return new JsonResult(200, message, nextUrl);
}
protected JsonResult successResult(String message, Object data) {
return new JsonResult(200, message, data);
}
protected JsonResult errorResult(String message) {
return new JsonResult(300, message);
}
以上傳回的json格式在web互動的時候已經很精簡了,而且封裝的很不錯
筆者最近需要對特定的web接口進行封裝,封裝成計費的API,這個時候上面格式裡面的json節點顯得多餘
"data":
{
"code":200,
"uuid":"xxxxx"
}
于是筆者想到了Spring裡面的ResponseEntity類
重構代碼
@PostMapping(value = "offline")
@ResponseBody
public ResponseEntity<Map<String,Object>> offline(){
if(StringUtils.isEmpty(apiKey)||StringUtils.isEmpty(apiKey))
{
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ImmutableMap.of("message","apiKey和appId不能為空","code",300));
}
return ResponseEntity.ok().body(ImmutableMap.of("message","轉換成功","code",200,"uuid",conversionId));
}
代碼簡潔了很多,傳回的json如下
其他方法
使用Jackson的@JsonIgnore,輸出到用戶端時将屏蔽這個字段
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.base.Objects;
import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Table(name = "data_bytedance")
public class ByteDanceData {
@Id
@GeneratedValue(generator = "JDBC")
@JsonIgnore
private Integer id;
private Integer distId;
@SerializedName("confirm")
private Integer confirm;
@JsonIgnore
@SerializedName("suspect")
private Integer suspect;
@SerializedName("dead")
private Integer dead;
@SerializedName("heal")
private Integer heal;
private float weight;
@JsonIgnore
@Transient
private String level;
private String updateTime;
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ByteDanceData data = (ByteDanceData) o;
return Objects.equal(distId, data.distId) &&
Objects.equal(confirm, data.confirm) &&
("area".equals(level)?
Objects.equal(suspect, data.suspect):
true) &&
Objects.equal(dead, data.dead) &&
Objects.equal(heal, data.heal);
}
@Override
public int hashCode() {
return 0;
}
}