NutzCN Logo
问答 自定义动作链导致@AdaptBy(type = JsonAdaptor.class) 未获取到值
发布于 1838天前 作者 Hamming 1511 次浏览 复制 上一个帖子 下一个帖子
标签:

自定义动作链导致@AdaptBy(type = JsonAdaptor.class) 未获取到值
在未自定义动作链 是 我的方法是能正确取到值代码如下


/** * 修改保存服务费配置 */ @At @POST @Ok("json") @RequiresPermissions("app:serviceFees:edit") @Slog(tag="服务费配置", after="修改保存服务费配置") @AdaptBy(type = JsonAdaptor.class) public Object editDo(@Param("serviceFees") ServiceFees serviceFees, @Param("items")ServiceFeeItem[] items,HttpServletRequest req) { try { if(Lang.isNotEmpty(serviceFees)){ if(serviceFees.isDefaults()){ List<ServiceFees> list = serviceFeesService.query(); list.forEach(sf->{ sf.setDefaults(false); serviceFeesService.update(sf); }); } serviceFees.setUpdateBy(ShiroUtils.getSysUserId()); serviceFees.setUpdateTime(new Date()); serviceFeesService.update(serviceFees); // if(Lang.isNotEmpty(items) && Lang.isNotEmpty(serviceFees)) { // serviceFees.setServiceFeeItemList(Arrays.asList(items)); // serviceFeesService.dao().updateWith(serviceFees,"serviceFees"); // serviceFeesService.dao().updateLinks(serviceFees.getServiceFeeItemList(), "serviceFees"); // } } return Result.success("system.success"); } catch (Exception e) { return Result.error("system.error"); } }

自定义动作链后 取不到值 这个是为什么呢 是不是少了什么

package io.nutz.nutzsite.common.mvc;

import io.nutz.nutzsite.common.mvc.processor.ErrorProcessor;
import io.nutz.nutzsite.common.mvc.processor.GlobalsSettingProcessor;
import io.nutz.nutzsite.common.mvc.processor.XssSqlFilterProcessor;
import org.nutz.integration.shiro.NutShiroProcessor;
import org.nutz.mvc.*;
import org.nutz.mvc.impl.NutActionChain;
import org.nutz.mvc.impl.processor.*;
import org.nutz.plugins.validation.ValidationProcessor;

import java.util.ArrayList;
import java.util.List;

/**
 * 动作链 配置
 * https://nutzam.com/core/mvc/action_chain.html
 * @Author: Haimming
 * @Date: 2019-10-09 11:41
 * @Version 1.0
 */
public class MyActionChainMaker implements ActionChainMaker {

    /**
     * 该接口只有一个方法
     * @param config
     * @param ai
     * @return
     */
    @Override
    public ActionChain eval(NutConfig config, ActionInfo ai) {
        // 提醒: config可以获取ioc等信息, ai可以获取方法上的各种配置及方法本身
        // 正常处理的列表
        List<Processor> list = new ArrayList<>();
        // 设置base/msg等内置属性
        list.add(new UpdateRequestAttributesProcessor());

        list.add(new GlobalsSettingProcessor());
        // 设置编码信息@Encoding
        list.add(new EncodingProcessor());
        // 获取入口类的对象,从ioc或直接new
        list.add(new ModuleProcessor());
        //shiro
        list.add(new NutShiroProcessor());
        // SQL 注入过滤 XSS过滤
        list.add(new XssSqlFilterProcessor());
        // 处理@Filters
        list.add(new ActionFiltersProcessor());
        // 处理@Adaptor
        list.add(new AdaptorProcessor());
        //必填项做判断
        list.add(new ValidationProcessor());
        // 执行入口方法
        list.add(new MethodInvokeProcessor());
        // 对入口方法进行渲染@Ok
        list.add(new ViewProcessor());
        for (Processor p : list) {
            try {
                p.init(config, ai);
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }

        // 最后是专门负责兜底的异常处理器,这个处理器可以认为是全局异常处理器,对应@Fail
        ErrorProcessor error = new ErrorProcessor();
        try {
            error.init(config, ai);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return new NutActionChain(list, error, ai);
    }
}

求大神指点

10 回复

是不是自定义的处理器读了body?

body只能读一次,所以需要变通一下

处理器读取完之后,把内容通过req.setAttr放入req对象,然后在JsonAdapter里面判断一下,有就直接用,没有就默认流程

定义了GlobalsSettingProcessor,XssSqlFilterProcessor
找到问题了
任意一个启用 都会导致 @AdaptBy(type = JsonAdaptor.class) 取不到值

呃 怎么解决这个问题呢
有没有案例

未启用任何 MyActionChainMaker 请求如下

Request URL: http://127.0.0.1:8080/app/serviceFees/editDo
Referrer Policy: no-referrer-when-downgrade
Accept: application/json, text/javascript, */*; q=0.01
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Connection: keep-alive
Content-Length: 993
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Cookie: Hm_lvt_d3f0f80430e686f09e671b88b927fa01=1575531301,1575618557,1575962166,1576028856; sid=30rqjga3aej6uqd264016hcup6; Hm_lpvt_d3f0f80430e686f09e671b88b927fa01=1576054352
Host: 127.0.0.1:8080
Origin: http://127.0.0.1:8080
Referer: http://127.0.0.1:8080/app/serviceFees/edit/7278ffb0d1ab4f5f997616d6d90709c8
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36
X-Requested-With: XMLHttpRequest
{"serviceFees":{"id":"7278ffb0d1ab4f5f997616d6d90709c8","name":"默认费率","landlordHalfAYear":"0.97","landlordAYear":"0.9","brokerageFee":"0.9","defaults":1},"items":[{"id":"d84c61fd82d2407e98d47a9412d0ca64","name":"月付","rate":1.125,"serviceFeesId":"7278ffb0d1ab4f5f997616d6d90709c8","updateBy":"1","updateTime":1565838117000,"value":1,"_index":0},{"id":"87bafc6b0a554af89320ae95fc1ef6ad","name":"季付","rate":1.1,"serviceFeesId":"7278ffb0d1ab4f5f997616d6d90709c8","updateBy":"1","updateTime":1565838263000,"value":3,"_index":1},{"createBy":"1","createTime":1565602210000,"id":"3bd113782950422ca80e79fef0413b59","name":"半年付","rate":1.05,"serviceFeesId":"7278ffb0d1ab4f5f997616d6d90709c8","updateBy":"1","updateTime":1565838279000,"value":6,"_index":2},{"createBy":"1","createTime":1565602210000,"id":"dec30ab98399431987f66e04637c18cc","name":"年付","rate":1,"serviceFeesId":"7278ffb0d1ab4f5f997616d6d90709c8","updateBy":"1","updateTime":1565838288000,"value":12,"_index":3}]}: 
Request URL: http://127.0.0.1:8080/app/serviceFees/editDo
Referrer Policy: no-referrer-when-downgrade
Accept: application/json, text/javascript, */*; q=0.01
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Connection: keep-alive
Content-Length: 993
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Cookie: Hm_lvt_d3f0f80430e686f09e671b88b927fa01=1575531301,1575618557,1575962166,1576028856; sid=4uk6dgilgaiccrn90d75jack9r; Hm_lpvt_d3f0f80430e686f09e671b88b927fa01=1576054512
Host: 127.0.0.1:8080
Origin: http://127.0.0.1:8080
Referer: http://127.0.0.1:8080/app/serviceFees/edit/7278ffb0d1ab4f5f997616d6d90709c8
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36
X-Requested-With: XMLHttpRequest
{"serviceFees":{"id":"7278ffb0d1ab4f5f997616d6d90709c8","name":"默认费率","landlordHalfAYear":"0.97","landlordAYear":"0.9","brokerageFee":"0.9","defaults":1},"items":[{"id":"d84c61fd82d2407e98d47a9412d0ca64","name":"月付","rate":1.125,"serviceFeesId":"7278ffb0d1ab4f5f997616d6d90709c8","updateBy":"1","updateTime":1565838117000,"value":1,"_index":0},{"id":"87bafc6b0a554af89320ae95fc1ef6ad","name":"季付","rate":1.1,"serviceFeesId":"7278ffb0d1ab4f5f997616d6d90709c8","updateBy":"1","updateTime":1565838263000,"value":3,"_index":1},{"createBy":"1","createTime":1565602210000,"id":"3bd113782950422ca80e79fef0413b59","name":"半年付","rate":1.05,"serviceFeesId":"7278ffb0d1ab4f5f997616d6d90709c8","updateBy":"1","updateTime":1565838279000,"value":6,"_index":2},{"createBy":"1","createTime":1565602210000,"id":"dec30ab98399431987f66e04637c18cc","name":"年付","rate":1,"serviceFeesId":"7278ffb0d1ab4f5f997616d6d90709c8","updateBy":"1","updateTime":1565838288000,"value":12,"_index":3}]}: 

Content-Type 莫名其妙就变了

js请求 调整如下 就能接收到了
需要指定 contentType 和dataType

            saveJson: function (url, data) {
                $.modal.loading("正在处理中,请稍后...");
                var config = {
                    url: url,
                    type: "post",
                    contentType:"application/json; charset=utf-8",
                    dataType: "json",
                    data: JSON.stringify(data),
                    success: function (result) {
                        $.operate.successCallback(result);
                    }
                };
                $.ajax(config)
            },

为什么这样 只能接收到空对象

Request URL: http://127.0.0.1:8080/app/serviceFees/addDo
Referrer Policy: no-referrer-when-downgrade
Accept: application/json, text/javascript, */*; q=0.01
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Connection: keep-alive
Content-Length: 485
Content-Type: application/json; charset=UTF-8
Cookie: Hm_lvt_d3f0f80430e686f09e671b88b927fa01=1576113157; sid=qhcomael9ghfko22e5fotd60so; rememberMe=trLsK+X3unYjDiGCL/9SdNnYLRnZxQli4mcNmZJENggk33SAqUlapP5iw00aDems40YMAsAozouXNzRfKHWeKWYBi3EPrscnMKSzWlZVn3FEJjQyKxBo4Zdgt06N9J+Ii+HARsxDD6srEq82wqASLDjJy00G8jOn/EavD8q/LppNfk3/eosA1Mm2uj2tn+nSjc9isk3yF7OcLut/FYLWVQN6Y0WQzNB2OH0xEmtXVgwdKbYPfYT4dcAo/vMx1W9JuMWtGGvJ2ZT+0fyOsPfpjNXNLtj6zIdkx8rrWsFFYeteTY8xUPpund5Ic7/9/axWDT79XJs2aa8pK7vidT553d8lVXobs8fGEyGYunmT0ETl3hhUyJIlizqfSC3nwEyEQSYuoYb1S/S/nu7lBu5BF0BbpHHN84WFm95fsAngQT7fTaP4HZcC/ajFvjBBPHPJXAOwEkS8hLuPx9DZN7eFUXTSqNebDWNcOst/QybA2nwDJpY66MABMSTVNugwK0f1NpfAjcj+u5VXnywxsSjcteQSn6iP4+Lq17W0N9WzImCJ0NLkrV+Ty7dMn3YonOmPrD/q40+cRQTw3caIyrr6wSZE6g473yZHYtu4Vtw1btSoND8BJT/drt1clQlrsRwBMD5KJuxdCQFC7BrTHu2qkDXoQ6DezQP1y57ac6KW9cAtYzElSGCb1O4TFGBcoKzilrIaWUwhnipp4kEEMFG6Z0YqxLjrFkMUyK3BXiK3R4a193e8CEzFmKJdPehFWjjeqKkAWlzffAcYibghdbLBBkdp4EbDF18zHKximw4tECa1nHo9hMvvC/L78PwbysWI2AmM1t08c7WH2Lxj/OiwIVmPZgAp6f3Aqgi2OkR7RX2HQi0ABUbGugyZBAVummTUo0PqK6b0zgzdWgX4rRUH0ZBrx5GirWTgBJqTydNv/b0=; Hm_lpvt_d3f0f80430e686f09e671b88b927fa01=1576113934
Host: 127.0.0.1:8080
Origin: http://127.0.0.1:8080
Referer: http://127.0.0.1:8080/app/serviceFees/add
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36
X-Requested-With: XMLHttpRequest
{"serviceFees":{"name":"1","landlordHalfAYear":"1","landlordAYear":"1","brokerageFee":"1","defaults":0},"items":[{"id":1,"name":"月付","value":1,"rate":1,"_index":0},{"id":2,"name":"季付","value":3,"rate":1,"_index":1},{"id":101,"name":"半年付","value":6,"rate":1,"_index":2},{"id":102,"name":"年付","value":12,"rate":1,"_index":3},{"id":103,"name":"2年付","value":24,"rate":1,"_index":4}]}
No properties

这个就能接收到对象

Request URL: http://127.0.0.1:8080/app/serviceFees/editDo
Referrer Policy: no-referrer-when-downgrade
Accept: application/json, text/javascript, */*; q=0.01
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Connection: keep-alive
Content-Length: 993
Content-Type: application/json; charset=UTF-8
Cookie: Hm_lvt_d3f0f80430e686f09e671b88b927fa01=1576113157; sid=qhcomael9ghfko22e5fotd60so; rememberMe=trLsK+X3unYjDiGCL/9SdNnYLRnZxQli4mcNmZJENggk33SAqUlapP5iw00aDems40YMAsAozouXNzRfKHWeKWYBi3EPrscnMKSzWlZVn3FEJjQyKxBo4Zdgt06N9J+Ii+HARsxDD6srEq82wqASLDjJy00G8jOn/EavD8q/LppNfk3/eosA1Mm2uj2tn+nSjc9isk3yF7OcLut/FYLWVQN6Y0WQzNB2OH0xEmtXVgwdKbYPfYT4dcAo/vMx1W9JuMWtGGvJ2ZT+0fyOsPfpjNXNLtj6zIdkx8rrWsFFYeteTY8xUPpund5Ic7/9/axWDT79XJs2aa8pK7vidT553d8lVXobs8fGEyGYunmT0ETl3hhUyJIlizqfSC3nwEyEQSYuoYb1S/S/nu7lBu5BF0BbpHHN84WFm95fsAngQT7fTaP4HZcC/ajFvjBBPHPJXAOwEkS8hLuPx9DZN7eFUXTSqNebDWNcOst/QybA2nwDJpY66MABMSTVNugwK0f1NpfAjcj+u5VXnywxsSjcteQSn6iP4+Lq17W0N9WzImCJ0NLkrV+Ty7dMn3YonOmPrD/q40+cRQTw3caIyrr6wSZE6g473yZHYtu4Vtw1btSoND8BJT/drt1clQlrsRwBMD5KJuxdCQFC7BrTHu2qkDXoQ6DezQP1y57ac6KW9cAtYzElSGCb1O4TFGBcoKzilrIaWUwhnipp4kEEMFG6Z0YqxLjrFkMUyK3BXiK3R4a193e8CEzFmKJdPehFWjjeqKkAWlzffAcYibghdbLBBkdp4EbDF18zHKximw4tECa1nHo9hMvvC/L78PwbysWI2AmM1t08c7WH2Lxj/OiwIVmPZgAp6f3Aqgi2OkR7RX2HQi0ABUbGugyZBAVummTUo0PqK6b0zgzdWgX4rRUH0ZBrx5GirWTgBJqTydNv/b0=; Hm_lpvt_d3f0f80430e686f09e671b88b927fa01=1576114152
Host: 127.0.0.1:8080
Origin: http://127.0.0.1:8080
Referer: http://127.0.0.1:8080/app/serviceFees/edit/7278ffb0d1ab4f5f997616d6d90709c8
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36
X-Requested-With: XMLHttpRequest
{,…}
items: [{id: "d84c61fd82d2407e98d47a9412d0ca64", name: "月付", rate: 1.125,…},…]
serviceFees: {id: "7278ffb0d1ab4f5f997616d6d90709c8", name: "默认费率", landlordHalfAYear: "0.97", landlordAYear: "0.9",…}

找到原因了 JSON.stringify(data)了两次数据

添加回复
请先登陆
回到顶部