模板式代码结构

目录结构

请求参数

package com.norm.request;

import java.io.Serializable;

/**
 * 请求实体顶级父类
 */
public class WooRequest implements Serializable {
}

 

package com.norm.request;

import com.annotation.validation.annotation.BigDecimalValidate;
import com.annotation.validation.annotation.DateValidate;
import com.annotation.validation.annotation.FieldValidate;

/**
 * 具体业务请求实体类
 */
public class FinanceRequest extends WooRequest {

    @FieldValidate(max = 20,isEmpty = false)
    private String applicationId;

    @FieldValidate(max = 20,isEmpty = false)
    private String cusName;

    @FieldValidate(max = 20,isEmpty = false)
    private String cusId;

    @FieldValidate(max = 20,isEmpty = false)
    @BigDecimalValidate
    private String finAmount;

    @FieldValidate(max = 20,isEmpty = false)
    @DateValidate
    private String finDate;

    public String getApplicationId() {
        return applicationId;
    }

    public void setApplicationId(String applicationId) {
        this.applicationId = applicationId;
    }

    public String getCusName() {
        return cusName;
    }

    public void setCusName(String cusName) {
        this.cusName = cusName;
    }

    public String getCusId() {
        return cusId;
    }

    public void setCusId(String cusId) {
        this.cusId = cusId;
    }

    public String getFinAmount() {
        return finAmount;
    }

    public void setFinAmount(String finAmount) {
        this.finAmount = finAmount;
    }

    public String getFinDate() {
        return finDate;
    }

    public void setFinDate(String finDate) {
        this.finDate = finDate;
    }
}

响应报文

package com.norm.response;

import java.io.Serializable;

/**
 * 响应实体顶级父类
 */
public class WooResponse implements Serializable {
}
package com.norm.response;

import com.norm.utils.Constants;

/**
 * 具体业务响应实体类
 */
public class FinanceResponse extends WooResponse{

    private String status;

    private String message;

    private Object result;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

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

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }

    public FinanceResponse(String status, String message, Object result) {
        this.status = status;
        this.message = message;
        this.result = result;
    }

    public static FinanceResponse responseSuccess(Object result){
        return new FinanceResponse(Constants.WOO_RESPONSE.SUCCESS.getStatus(),Constants.WOO_RESPONSE.SUCCESS.getMessage(),result);
    }

    public static FinanceResponse responseFail(Object result){
        return new FinanceResponse(Constants.WOO_RESPONSE.FAIL.getStatus(),Constants.WOO_RESPONSE.FAIL.getMessage(),result);
    }
}

工具常量类

package com.norm.utils;

/**
 * 常量类
 */
public class Constants {
    public static final String SUCCESS = "success";

    public enum WOO_RESPONSE{

        SUCCESS("1","请求处理成功"),
        FAIL("0","请求处理失败");

        private String status;
        private String message;

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getMessage() {
            return message;
        }

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

        WOO_RESPONSE(String status, String message) {
            this.status = status;
            this.message = message;
        }
    }
}

基础业务处理类

package com.norm.service;

import com.annotation.validation.utils.ValidateUtil;
import com.norm.request.WooRequest;
import com.norm.response.FinanceResponse;
import com.norm.response.WooResponse;
import java.util.Map;

/**
 * 基础业务处理类
 */
public abstract class BaseService {

    public WooResponse validateRequest(WooRequest request,RequestProcessor processor){
        Map<String, Object> validateMap = ValidateUtil.validateJson(request, String.format("调用接口[%s]参数有误", request.getClass()));
        if((Boolean) validateMap.get("result")){
            WooResponse wooResponse = processor.processRequest(request);
            return wooResponse;//返回接口处理后的参数;
//            return FinanceResponse.responseSuccess(null);//返回无参数
        }else{
            return FinanceResponse.responseFail(null);
        }
    }

    /**
     * 业务处理器:各模块实现该接口处理各自的业务逻辑
     */
    public interface RequestProcessor{
        WooResponse processRequest(WooRequest request);
    }

    /**
     * 展示map 数据
     * @param map
     * @return
     */
    public String dispaly(Map<String,Object> map){
        map.forEach((key,value)->
        System.out.println("key:"+key+",value:"+value));
        return null;
    }
}

具体业务处理类

package com.norm.service;

import com.norm.response.WooResponse;

public interface FinanceService {

    WooResponse financeQuery(String reqDate);
}
package com.norm.service.impl;

import com.norm.request.FinanceRequest;
import com.norm.request.WooRequest;
import com.norm.response.FinanceResponse;
import com.norm.response.WooResponse;
import com.norm.service.BaseService;
import com.norm.service.FinanceService;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

@Service
public class FinanceServiceImpl extends BaseService implements FinanceService {

    @Override
    public WooResponse financeQuery(String reqDate) {
        FinanceRequest request = null;
        try{
            request = (FinanceRequest)JSONObject.toBean(JSONObject.fromObject(reqDate),FinanceRequest.class);
        }catch (Exception e){
            e.printStackTrace();
        }
        return validateRequest(request, new RequestProcessor() {
            @Override
            public WooResponse processRequest(WooRequest request) {
                FinanceRequest financeRequest = (FinanceRequest) request;
                //进行业务处理
                return FinanceResponse.responseSuccess("成功了");
            }
        });
    }

}

控制器调用类

package com.norm.controller;

import com.norm.service.FinanceService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;

@Controller
@RequestMapping(value = "fin")
public class FinanceController  {

    @Resource
    private FinanceService financeService;

    /**
     * @return
     */
    @RequestMapping(value = "financeQuery")
    public String financeQuery(String reqDate){
        return financeService.financeQuery(reqDate).toString();
    }

}

注解类校验详见:this

规范化代码,看起来更凡尔赛。。。

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值