BaseResponse.Class
@ApiIgnore
@Data
@JsonInclude(value = JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class BaseResponse<T> {
/**
* 响应编码
*/
@ApiModelProperty(hidden = true)
private int code;
/**
* 响应消息
*/
@ApiModelProperty(hidden = true)
private String message;
/**
* 是否可见
*/
@ApiModelProperty(hidden = true)
private boolean visible;
@ApiModelProperty(hidden = true)
private BaseException exception;
/**
* 分页对象
*/
@ApiModelProperty(hidden = true)
public Paging paging;
/**
* 业务对象
*/
public T data;
@ApiModelProperty(hidden = true)
@JsonProperty("extra")
public Map<String, ?> extraMap;
public BaseResponse() {
this(null, null);
}
public BaseResponse(T t) {
this(t, null);
}
public BaseResponse(T t, Map<String, ?> extraMap) {
if (null == t) {
this.code = HttpStatus.OK.value();
this.message = HttpStatus.OK.name();
} else if (t instanceof Converter) {
this.code = HttpStatus.OK.value();
this.message = HttpStatus.OK.name();
data = t;
} else if (t instanceof BaseModel) {
this.code = HttpStatus.OK.value();
this.message = HttpStatus.OK.name();
data = t;
} else if (t instanceof List) {
this.code = HttpStatus.OK.value();
this.message = HttpStatus.OK.name();
@SuppressWarnings({ "unchecked", "rawtypes" })
PageInfo<?> pageInfo = new PageInfo((List) t);
paging = new Paging(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());
data = t;
} else if(t instanceof Map){
this.code = HttpStatus.OK.value();
this.message = HttpStatus.OK.name();
data = t;
} else if (t instanceof BadRequestException) {
this.code = HttpStatus.BAD_REQUEST.value();
// this.message = HttpStatus.BAD_REQUEST.name();
this.message = ((BadRequestException)t).getMessageList().get(0);
this.visible = true;
// this.exception = (BadRequestException) t;
this.exception = null;
} else if (t instanceof InternalServerErrorException) {
this.code = HttpStatus.INTERNAL_SERVER_ERROR.value();
this.message = HttpStatus.INTERNAL_SERVER_ERROR.name();
this.visible = false;
// this.exception = (InternalServerErrorException) t;
} else if (t instanceof OpenAPIException) {
OpenAPIException exception = ((OpenAPIException) t);
this.code = exception.getHttpErrorCode();
this.message = exception.toString();
this.visible = false;
this.exception = null;
} else if (t instanceof BatchResponse) {
this.code = HttpStatus.OK.value();
this.message = HttpStatus.OK.name();
data = t;
} else if (t instanceof Exception) {
this.code = HttpStatus.UNAUTHORIZED.value();
this.message = ((OpenAPIException) t).toString();
this.visible = false;
this.exception = null;
} else {
this.code = HttpStatus.OK.value();
data = t;
}
this.extraMap = extraMap;
}
public Paging getPaging() {
return paging;
}
public void setPaging(Paging paging) {
this.paging = paging;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Map<String, ?> getExtraMap() {
return extraMap;
}
public void setExtraMap(Map<String, ?> extraMap) {
this.extraMap = extraMap;
}
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 boolean isVisible() {
return visible;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
public BaseException getException() {
return exception;
}
public void setException(BaseException exception) {
this.exception = exception;
}
public class Paging {
private int pageNum;
private int pageSize;
private long total;
private Paging(int pageNum, int pageSize, long total) {
this.pageNum = pageNum;
this.pageSize = pageSize;
this.total = total;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
}
}
Converter.Interface
@FunctionalInterface
public interface Converter<S, T>{
T convert(S s);
default List<T> convertToList(final List<S> input) {
if(input instanceof Page){
Page<T> page = new Page<>();
if(!CollectionUtils.isEmpty(input)) {
page.addAll(input.stream().map(this::convert).collect(toList()));
}
page.setTotal(((Page) input).getTotal());
page.setPageNum(((Page) input).getPageNum());
page.setPageSize(((Page) input).getPageSize());
return page;
}else if(CollectionUtils.isEmpty(input)){
return Collections.emptyList();
}else{
return input.stream().map(this::convert).collect(toList());
}
}
}
ConsumerAppUserResponse.Class
@Data
public class ConsumerAppUserResponse implements Converter<ConsumerAppUser, ConsumerAppUserResponse> {
private Integer id;
/**
* 手机号
*/
private String mobile;
/**
* 真实姓名
*/
private String realname;
/**
* 邮箱
*/
private String email;
/**
* 企业email
*/
private String orgEmail;
/**
* 如果要创建企微用户,需要传递该属性信息
*/
private List<WxworkCorpUserResponse> wxworkCorpUserList;
@Override
public ConsumerAppUserResponse convert(ConsumerAppUser consumerAppUser) {
if(null == consumerAppUser){
return null;
}
ConsumerAppUserResponse t = new ConsumerAppUserResponse();
BeanUtils.copyProperties(consumerAppUser, t, "wxworkCorpUserList");
if(CollectionUtils.isNotEmpty(consumerAppUser.getWxworkCorpUserList())){
t.setWxworkCorpUserList(new WxworkCorpUserResponse().convertToList(consumerAppUser.getWxworkCorpUserList()));
}
return t;
}
}
ConsumerAppUser.Class
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
@JsonInclude(value = Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class ConsumerAppUser extends BaseMultiTenantModel {
/**
* 手机号
*/
private String mobile;
/**
* 真实姓名
*/
private String realname;
/**
* 邮箱
*/
private String email;
/**
* 微信OpenId,只用于微信登录的时候传递参数,不在数据库持久化保存
*/
private transient String openId;
/**
* 微信公众号AppID,只用于微信登录的时候传递参数,不在数据库持久化保存
*/
private transient String appId;
/**
* 类型
*/
private Integer status;
/**
* 头像地址
*/
private String headPortraitUrl;
/**
* 最后一次登录时间
*/
private LocalDateTime lastLoginTime;
/**
* 飞书租户id
*/
private String feishuTenantKey;
/**
* 飞书用户userId
*/
private String feishuUserId;
/**
* welink租户id
*/
private String welinkTenantId;
/**
* welink用户id
*/
private String welinkUserId;
/**
* dingding 租户id, 实际取的是CorpId
*/
private String dingTenantId;
/**
* dingding 用户id
*/
private String dingUserId;
/**
* 企业email
*/
private String orgEmail;
/**
* 用于存储关联的企微用户信息
*/
private transient List<WxworkCorpUser> wxworkCorpUserList;
public Optional<String> decideMobileOrEmailOrNameByFieldType(Integer fieldType){
return Optional.ofNullable(fieldType).flatMap(e -> decideMobileOrEmailOrNameByFieldType(FieldType.Type.valueOf(e)));
}
public Optional<String> decideMobileOrEmailOrNameByFieldType(FieldType.Type type) {
if(null == type){
return Optional.empty();
}
switch (type){
case TELEPHONE:
return Optional.ofNullable(mobile);
case EMAIL:
return Optional.ofNullable(email);
case SINGLE_LINE:
return Optional.ofNullable(realname);
default:
return Optional.empty();
}
}
public Optional<String> decideMobileOrEmail(FieldType.Type type) {
if(null == type){
return Optional.empty();
}
switch (type){
case TELEPHONE:
return Optional.ofNullable(mobile);
case EMAIL:
return Optional.ofNullable(email);
default:
return Optional.empty();
}
}
}
Controller
@GetMapping(consumes = APPLICATION_JSON, produces = APPLICATION_JSON)
public ResponseEntity<BaseResponse<ConsumerAppUserResponse>> search(@RequestParam(value = "mail", required = false) final String mail,
@RequestParam(value = "phone", required = false) final String phone) {
ConsumerAppUser consumerAppUser = consumerAppUserWithWxworkCorpUserService.findByEmailOrPhone(mail, phone);
ConsumerAppUserResponse consumerAppUserResponse = new ConsumerAppUserResponse().convert(consumerAppUser);
BaseResponse<ConsumerAppUserResponse> consumerAppUserResponseBaseResponse = new BaseResponse<>(consumerAppUserResponse);
ResponseEntity<BaseResponse<ConsumerAppUserResponse>> baseResponseResponseEntity = new ResponseEntity<>(consumerAppUserResponseBaseResponse, HttpStatus.OK);
return baseResponseResponseEntity;
}
标签:code,return,String,java,封装,HttpStatus,private,返回值,public
From: https://www.cnblogs.com/AlwaysStudyingLiuNing/p/17218809.html