springboot返回类型封装

controller类:
/**
* 获取店铺信息
* 根据城市编码查询当前城市下 根据定位的经纬度获取最近的一个门店信息
* @param cityCode 当前城市编码
* @return 返回符合门店配送条件的门店配送信息 没有匹配到会返回null
*/
@RequestMapping ( value = [ "/sellershop/list" ])
@ApiMethod ( id = "getSellerShopAddress" , summary = "getSellerShopAddress" , description = "获取店铺信息" )
fun getSellerShopAddress( @ApiQueryParam ( name = "cityCode" , description = "当前定位的城市编码" , required = false )
@RequestParam ( value = "cityCode" , required = false )
cityCode: String?): ResponseEntity<YTXResponse> {
val result = YTXResponse.success()

val sellerDistributtionVoList: MutableList<SellerDistributtionVo> = ArrayList()
if (!(cityCode. equals ( "" ) || cityCode == null )) {
//根据cityCode获取当前城市下的门店及配送范围信息
val distributionScopeList = distributionScopeManager .findBycityCode(cityCode)
//循环门店配送信息 添加门店经纬度 门店名字 及门店所在地城市code name
if (distributionScopeList. isNotEmpty ()) {
for (distributionScope in distributionScopeList) {
val sellerDistributtionVo = SellerDistributtionVo()
//配送区域
sellerDistributtionVo. areas = distributionScope. areas
//门店id
sellerDistributtionVo. sellerAccountId = distributionScope. sellerAccountId
val sellerAddress = sellerManager .findStoreAddressByAccountId(distributionScope. sellerAccountId )
if (sellerAddress != null ) {
//经纬度
sellerDistributtionVo. latitude = sellerAddress. latitude
sellerDistributtionVo. longitude = sellerAddress. longitude
if (sellerAddress. cityCode != null ) {
val region = regionManager .findByCode(sellerAddress. cityCode )
//城市name
if (region != null ) {
//城市code
sellerDistributtionVo. cityCode = region. cityCode
sellerDistributtionVo. cityName = region. name
}
}
}
//门店名字
val seller = sellerManager .findByAccountId(distributionScope. sellerAccountId )
if (seller != null ) sellerDistributtionVo. sellerAccountName = seller . sellerShop . name
sellerDistributtionVoList.add(sellerDistributtionVo)
}
}
}
//找到所有有门店的城市 的code
val allCitiesHavingStores = sellerManager .findAllCitiesHavingStores()
val regionList: MutableList<Region> = ArrayList()
//添加城市名称
allCitiesHavingStores. mapTo (regionList) { regionManager .findByCode( it ) }

val simplePropertyPreFilter = SimplePropertyPreFilter(Region:: class . java , "cityCode" , "name" )
result.addData( "regionList" , JSON.toJSONString(regionList, simplePropertyPreFilter))
result.addData( "distributionScopeList" , sellerDistributtionVoList)
return ResponseEntity(result, HttpStatus. OK )
}
YTXResponse 类:
import java.util.LinkedHashMap;
import java.util.Map;
/**
* 与客户端交互的返回结果
*/
public class YTXResponse implements YTXMessage, YTXData {
/**
* 操作成功与否 由后台返回前台
*/
private String success = SuccessFlag. TRUE .toString();
/**
* 处理信息代码
*/
private String code = "Y00-000200" ;

/**
* 信息
*/
private String message = "SUCCESS" ;

/**
* 返回客户端所带的数据
*/
private Map<String, Object> data = new LinkedHashMap<>();

public static YTXResponse success() {
YTXResponse response = new YTXResponse();
response. success = SuccessFlag. TRUE .toString();
response. message = "SUCCESS" ;
return response;
}

@Override
public String getSuccess() {
return success ;
}

public void setSuccessFalse() {
this . success = SuccessFlag. FALSE . value ;
}

public void setSuccessTrue() {
this . success = SuccessFlag. TRUE . value ;
}

/**
* 操作是不是成功
* @return true:成功, false:未成功
*/
public Boolean isSucceed() {
return SuccessFlag. TRUE .toString().equals( success );
}

/**
* 操作是不是失败
* @return true:失败, false:无误
*/
public Boolean isFailed() {
return !SuccessFlag. TRUE .toString().equals( success );
}

@Override
public void setSuccess(String success) {
this . success = success;
}

@Override
public String getCode() {
return code ;
}

@Override
public void setCode(String code) {
this . code = code;
}

@Override
public String getMessage() {
return message ;
}

@Override
public void setMessage(String message) {
this . message = message;
}

/**
* Return the underlying { @code data} instance (never { @code null}).
*/
@Override
public Map<String, Object> getData() {
if ( this . data == null ) {
this . data = new LinkedHashMap<>();
}
return this . data ;
}

@Override
@SuppressWarnings ( "unchecked" )
public < T > T getData(String dataName) {
if ( this . data == null ) {
this . data = new LinkedHashMap<>();
}
return ( T ) this . data .get(dataName);
}

@Override
@SuppressWarnings ( "unchecked" )
public < T > T addData(String dataName, T dataValue) {
if ( this . data == null ) {
this . data = new LinkedHashMap<>();
}
if (dataName == null ) {
throw new IllegalArgumentException( "Model attribute name must not be null" );
}
this . data .put(dataName, dataValue);
return dataValue;
}

public enum SuccessFlag {

TRUE ( "TRUE" ), FALSE ( "FALSE" );

String value ;

SuccessFlag(String value) {
this . value = value;
}

public String getValue() {
return value ;
}
}

public YTXResponse() {
}

public YTXResponse(String code, String message) {
this . code = code;
this . message = message;
}

public YTXResponse(String code, String message, Map<String, Object> data) {
this . code = code;
this . message = message;
this . data = data;
}

private YTXResponse(String success, String code, String message, Map<String, Object> data) {
this . success = success;
this . code = code;
this . message = message;
this . data = data;
}

public static Builder builder() {
return new Builder();
}

public static class Builder {
/**
* 操作成功与否 由后台返回前台
*/
private String success = SuccessFlag. TRUE .toString();

/**
* 处理信息代码
*/
private String code = "Y00-000200" ;

/**
* 信息
*/
private String message = "SUCCESS" ;

/**
* 返回客户端所带的数据
*/
private Map<String, Object> data = new LinkedHashMap<>();

private Builder() {
}

public Builder success(String success) {
this . success = success;
return this ;
}

public Builder code(String code) {
this . code = code;
return this ;
}

public Builder message(String message) {
this . message = message;
return this ;
}

public Builder data(Map<String, Object> data) {
this . data = data;
return this ;
}

@SuppressWarnings ( "unchecked" )
public < T > Builder addData(String dataName, T dataValue) {
if ( this . data == null ) {
this . data = new LinkedHashMap<>();
}
if (dataName == null ) {
throw new IllegalArgumentException( "Model attribute name must not be null" );
}
this . data .put(dataName, dataValue);
return this ;
}

@Override
public String toString() {
return "Builder{" +
"success='" + success + ' \' ' +
", code='" + code + ' \' ' +
", message='" + message + ' \' ' +
", data=" + data +
'}' ;
}

public YTXResponse build() {
return new YTXResponse( success , code , message , data );
}
}
}
YTXMessage类:
import java.util.List;
/**
* 与客户端交互时,返回的信息
*/
public interface YTXMessage {

/**
* 返回处理成功与否
* @return "true" or "false"
*/
String getSuccess();

/**
* 设置成功与否
* @param success 成功与否
*/
void setSuccess(String success);

/**
* 返回信息的统一代码
* @return code 统一代码
*/
String getCode();

/**
* 设置信息的统一代码
* @param code 信息的统一代码
*/
void setCode(String code);

/**
* 返回具体处理信息
* @return 具体处理信息
*/
String getMessage();

/**
* 设置具体信息
* @param message 具体处理信息
*/
void setMessage(String message);
}
YTXData类:
import java.util.Map;
public interface YTXData {
Map<String, Object> getData();

< T > T getData(String dataName);

< T > T addData(String dataName, T dataValue);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值