【spring】spring boot中使用@EnableTransactionManagement 以后,spring mvc接收前台ajax的post方法傳過來的參數,使用@RequestBody接收不到參數


在啟動類上添加了注解:

@EnableTransactionManagement,

 postMan測試接口,以這種方式傳遞參數:

測試結果:

 接收不到參數

 

 

問題解決:

原因:是因為 這個項目中的Controller層 其實是有一層接口層,一層實現層。

 

 其實controller層不應該有接口層,而直接就是 實現層。

像上面這種有接口層,又有實現層的設計,會導致在啟動類添加了@EnableTransactionManagement注解之后,導致

接口層:

package com.pisen.cloud.luna.ms.goods.api;

import com.pisen.cloud.luna.core.result.AjaxResult;
import com.pisen.cloud.luna.core.result.PageResult;
import com.pisen.cloud.luna.ms.goods.base.domain.GoodsAid;
import com.pisen.cloud.luna.ms.goods.base.domain.GoodsAidLog;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@RequestMapping("/ten/goodsAid")
public interface ITenGoodsAidApi extends ICRUDCommonApi<GoodsAid> {

    /**
     * 售后服務的更新操作
     *
     * 注意地址!!!!!!是 updated
     *
     * 服務未啟用-->可以修改商品分類和 服務信息
     * 服務已啟用-->不可以修改商品分類  可以修改服務信息
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/updated",method = RequestMethod.POST)
    AjaxResult<GoodsAid> updated(GoodsAid entity);

    /**
     * 啟用售后輔助服務配置
     *
     * 啟用以后不能再禁用或者刪除
     *
     * 所以禁用操作不提供
     * @param aid
     * @return
     */
    @RequestMapping(value = "/enable",method = RequestMethod.POST)
    AjaxResult<String> enable(GoodsAid aid);


    /**
     * 分頁查詢 售后服務日志
     *
     * @param entity
     *            實例對象
     * @return 標准返回對象
     */
    @RequestMapping(value = "/log/pageFind", method = RequestMethod.GET)
    PageResult<GoodsAidLog> pageFind(@RequestBody GoodsAidLog entity) ;


    /**
     * 新增實例 售后服務日志
     *
     * @param entity
     *            實例對象
     * @return 標准返回對象
     */
    @RequestMapping(value = "/log/insert", method = RequestMethod.POST)
    public AjaxResult<GoodsAidLog> insert(@RequestBody GoodsAidLog entity);

}
View Code

實現層:

package com.pisen.cloud.luna.ms.goods.api.impl;

import com.pisen.cloud.luna.core.reqmodal.RequestData;
import com.pisen.cloud.luna.core.result.AjaxResult;
import com.pisen.cloud.luna.core.result.LunaResultBean;
import com.pisen.cloud.luna.core.result.PageResult;
import com.pisen.cloud.luna.core.utils.LunaIDUtil;
import com.pisen.cloud.luna.core.utils.TenementUser;
import com.pisen.cloud.luna.ms.goods.api.ITenGoodsAidApi;
import com.pisen.cloud.luna.ms.goods.base.domain.GoodsAid;
import com.pisen.cloud.luna.ms.goods.base.domain.GoodsAidLog;
import com.pisen.cloud.luna.ms.goods.base.domain.GoodsBindConfigMapping;
import com.pisen.cloud.luna.ms.goods.base.service.GoodsAidService;
import com.pisen.cloud.luna.ms.goods.base.service.GoodsBindConfigMappingService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class TenGoodsAidApiImpl implements ITenGoodsAidApi {


    @Autowired
    GoodsAidService service;

    @Autowired
    GoodsBindConfigMappingService bindConfigMappingService;

    /**
     * 新增  售后輔助服務
     *
     * 默認新增服務是禁用的
     *
     * serviceEnter 保存 即以  常量,常量 字符串保存即可
     * 例如: 1,2,3,4  或  1,3,5
     * @param entity
     *            實例對象
     * @return
     */
    @Override
    public AjaxResult<GoodsAid> insert(@RequestBody GoodsAid entity) {
        LunaResultBean.checkField(entity,"name","floor","goodsTypeUidList",
                "serviceEnter","outerUrl","urlContext");

        AjaxResult<GoodsAid> res = new AjaxResult<>();
        //初始化 對象
        entity.initInsertData();
        //如果全局字段傳入 則采用傳入值  否則 默認非全局
        entity.setGlobal(entity.getGlobal()  == null ? GoodsAid.NOT_GLOBAL : entity.getGlobal());
        entity.setUid(LunaIDUtil.uid());
        GoodsAid save = service.save(entity);
        if (save != null){
            res.initTrue(save);
        }else {
            res.initFalse("保存失敗",LunaResultBean.ERROR_BUSINESS);
        }
        return res;
    }


    @Override
    public AjaxResult<GoodsAid> update(GoodsAid entity) {
        return null;
    }

    /**
     * 售后服務的更新操作
     *
     * 注意地址!!!!!!是 updated
     *
     * 服務未啟用-->可以修改商品分類和 服務信息
     * 服務已啟用-->不可以修改商品分類  可以修改服務信息
     *
     * @param entity
     * @return
     */
    @Override
    public AjaxResult<GoodsAid> updated(@RequestBody GoodsAid entity) {
        LunaResultBean.checkField(entity,"uid","name","floor","goodsTypeUidList","serviceEnter","outerUrl","urlContext");
        AjaxResult<GoodsAid> res = new AjaxResult<>();

        List<String> goodsTypeUid = entity.getGoodsTypeUidList();
        GoodsAid old = service.findByUid(entity);
        if (old != null){
            String uid = entity.getUid();
            Integer enabled = old.getEnabled();

            //驗證標志 標志是否禁用狀態 或 啟用狀態下 商品分類一致
            boolean flag = false;
            if (GoodsAid.CONFIG_DISENABLE == enabled){
                //服務 禁用狀態
                flag = true;
            }else {
                //服務 啟用狀態
                //查找售后服務綁定的原商品分類
                List<String> oldTypeList = service.findGoodsTypeUidsByConfigUid(uid);

                if (oldTypeList != null){
                    if (bindConfigMappingService.chechDiff(oldTypeList,goodsTypeUid)){
                        flag = true;
                    }else {
                        res.initFalse("服務已經啟用,不能更改商品分類",LunaResultBean.ERROR_BUSINESS);
                    }

                }else {
                    res.initFalse("原售后服務未綁定任何商品分類,屬於非法服務",LunaResultBean.ERROR_BUSINESS);
                }
            }

            //驗證通過
            if (flag){
                entity.initUpdateData();
                GoodsAid update = service.update(entity);
                if (update != null){
                    res.initTrue(update);
                }else {
                    res.initFalse("更新失敗",LunaResultBean.ERROR_BUSINESS);
                }
            }
        }else {
            res.initFalse("售后服務不存在",LunaResultBean.ERROR_BUSINESS);
        }
        return res;
    }

    /**
     *刪除  售后輔助服務
     *
     * 注意 啟用以后的服務就不能禁用或刪除了
     *      啟用之前的服務 可以刪除
     * @param entity
     *            實例對象
     * @return
     */
    @Override
    public AjaxResult<GoodsAid> delete(@RequestBody  GoodsAid entity) {
        LunaResultBean.checkField(entity,"uid");
        AjaxResult<GoodsAid> res = new AjaxResult<>();

        //當前租戶
        TenementUser tenementUser = RequestData.TENEMENT_USER.get();
        String tenementId = tenementUser.getTenementId();

        GoodsAid old = service.findByUid(entity);
        if (old != null){
            if (tenementId.equals(old.getTenementId())){
                if (old.getEnabled() == GoodsAid.CONFIG_DISENABLE){
                    boolean flag = service.delete(entity);
                    if (flag){
                        res.initTrue(entity);
                    }else {
                        res.initFalse("刪除失敗",LunaResultBean.ERROR_BUSINESS);
                    }
                }else {
                    res.initFalse("售后服務已啟用,不能刪除",LunaResultBean.ERROR_BUSINESS);
                }
            }else {
                res.initFalse("非法操作",LunaResultBean.ERROR_BUSINESS);
            }
        }else {
            res.initFalse("售后服務不存在",LunaResultBean.ERROR_BUSINESS);
        }
        return res;
    }
    /**
     * 分頁查詢 【售后輔助服務】
     *
     * @param entity
     *            實例對象
     * @return 標准返回對象
     */
    @Override
    public PageResult<GoodsAid> pageFind(GoodsAid entity) {
        //當前租戶
        TenementUser tenementUser = RequestData.TENEMENT_USER.get();
        String tenementId = tenementUser.getTenementId();
        entity.setTenementId(tenementId);

        Page<GoodsAid> page = service.pageFind(entity);
        PageResult<GoodsAid> res = new PageResult<>();
        res.initTrue(page.getTotalElements(),page.getContent());
        return res;
    }

    /**
     * 根據 售后字段  獲取信息
     * @param entity
     *            實例對象
     * @return
     */
    @Override
    public AjaxResult<List<GoodsAid>> find(GoodsAid entity) {
        return null;
    }

    /**
     * 啟用接口
     * 注意 啟用以后 就不能禁用了
     *
     * 所以沒有禁用接口和禁用操作
     * @param entity
     * @return
     */
    @Override
    public AjaxResult<String> enable(@RequestBody GoodsAid entity) {
        LunaResultBean.checkField(entity,"uid");

        AjaxResult<String> res = new AjaxResult<>();
        //當前租戶
        TenementUser tenementUser = RequestData.TENEMENT_USER.get();
        String tenementId = tenementUser.getTenementId();

        GoodsAid old = service.findByUid(entity);
        if (old != null){
            if (tenementId.equals(old.getTenementId())){
                Integer enabled = old.getEnabled();
                if (enabled == GoodsAid.CONFIG_DISENABLE){
                    String result = service.enable(old);
                    if (result.equals("啟用成功")){
                        res.initTrue(result);
                    }else {
                        res.initFalse(result,LunaResultBean.ERROR_BUSINESS);
                    }
                }else {
                    res.initFalse("售后服務已經啟用,不用重復啟用",LunaResultBean.ERROR_BUSINESS);
                }
            }else {
                res.initFalse("非法操作",LunaResultBean.ERROR_BUSINESS);
            }
        }else {
            res.initFalse("售后服務不存在",LunaResultBean.ERROR_BUSINESS);
        }

        return res;
    }


    /**
     * 分頁查詢 【售后服務日志】注意 是 日志接口
     *
     * 注意接口地址
     * /ten/goodsAid/log/pageFind
     * @param entity
     *            實例對象
     * @return 標准返回對象
     */
    @Override
    public PageResult<GoodsAidLog> pageFind(GoodsAidLog entity) {

        //當前租戶
        TenementUser tenementUser = RequestData.TENEMENT_USER.get();
        String tenementId = tenementUser.getTenementId();
        entity.setTenementId(tenementId);

        Page<GoodsAidLog> page = service.pageFindLog(entity);
        PageResult<GoodsAidLog> res = new PageResult<>();
        res.initTrue(page.getTotalElements(),page.getContent());
        return res;
    }

    /**
     * 新增實例 【售后服務日志】注意 是 日志接口
     * 注意接口地址!!!!!!!!!
     * /ten/goodsAid/log/insert
     *
     * 日志記錄只是做保存
     * @param entity
     *            實例對象
     * @return 標准返回對象
     */
    @Override
    public AjaxResult<GoodsAidLog> insert(@RequestBody GoodsAidLog entity) {
        LunaResultBean.checkField(entity,"goodsAidUid","memberId","securityCode","goodsId");
        AjaxResult<GoodsAidLog> res = new AjaxResult<>();

        // 當前租戶
        TenementUser tenementUser = RequestData.TENEMENT_USER.get();
        String tid = tenementUser.getTenementId();

        entity.setUid(LunaIDUtil.uid());
        entity.setTenementId(tid);

        GoodsAidLog result = service.saveLog(entity);
        if (result != null){
            res.initTrue(result);
        }else {
            res.initFalse("售后服務日志保存失敗",LunaResultBean.ERROR_BUSINESS);
        }

        return res;
    }
}
View Code

這樣的設計

 

導致 @RestController 注解寫在實現層並不能被解析到!!!!!!!

 

這也就導致了最上面的問題,命名 controller的實現方法 POST請求的加了@RequestBody ,前台也傳值了,但就是不能接收到值!!!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM