参数含有下划线开头无法正确传参问题

现象:

定义如下参数:

参数含有下划线开头无法正确传参问题_第1张图片

使用post传参:

参数含有下划线开头无法正确传参问题_第2张图片

 传递参数如下:

{"docName":"0290bf3bf9174f37a8984915d396fa94.png","examDate":"2022-01-12 00:00:00","examType":"4","examUserId":"1353881183645077506","examUserName":"科技科账号","examUserOrgId":"1243469076374843394","villageOid":"1384688","files":[{"docOid":"1497096946099105794","fileOid":"202202d35174f418a5447a8d7c918b6af2156a","fileUrl":"0290bf3bf9174f37a8984915d396fa94.png","name":"202202d35174f418a5447a8d7c918b6af2156a","url":"0290bf3bf9174f37a8984915d396fa94.png","uid":1646030867386,"status":"success"},{"fileOid":"2022021f1534d292c34ef5bf5a74519b204525","fileUrl":"Capture001.png","name":"2022021f1534d292c34ef5bf5a74519b204525","url":"Capture001.png","uid":1646030874168,"status":"success"}],"isSave":false,"_fileIds":"202202d35174f418a5447a8d7c918b6af2156a,2022021f1534d292c34ef5bf5a74519b204525","_fileUrls":"0290bf3bf9174f37a8984915d396fa94.png,Capture001.png"}

参数含有下划线开头无法正确传参问题_第3张图片

 此字段有对应值。

但是通过postman进行测试时会发现_fileIds和_fileUrls无法正确获取到对应值。

现象如下:

参数含有下划线开头无法正确传参问题_第4张图片

 其他测试:

输入:

{"_fileIds":"202202d35174f418a5447a8d7c918b6af2156a,2022021f1534d292c34ef5bf5a74519b204525",
"_fileUrls":"0290bf3bf9174f37a8984915d396fa94.png,Capture001.png"}

现象:

参数含有下划线开头无法正确传参问题_第5张图片

 输入:

{"__fileIds":"202202d35174f418a5447a8d7c918b6af2156a,2022021f1534d292c34ef5bf5a74519b204525",
"_fileUrls":"0290bf3bf9174f37a8984915d396fa94.png,Capture001.png"}

参数含有下划线开头无法正确传参问题_第6张图片

 具体原因不明。

========================================================================

怀疑一:json转化问题。

怀疑二:代码做了相应转化。

========================================================================

进行问题排查:

方向一:在过滤器和拦截器处设置断点,查看在过滤器和拦截器处是否正常传参。

下面的方法可以获取body参数:

    //通过request.getReader()的方式来获取body
    public static String getBodyMsg(HttpServletRequest request)
       {
    
           BufferedReader br = null;
           StringBuilder sb = new StringBuilder("");
           try
           {
               br = request.getReader();
               String str;
               while ((str = br.readLine()) != null)
               {
                   sb.append(str);
               }
               br.close();
           }
           catch (IOException e)
           {
               e.printStackTrace();
           }
           finally
           {
               if (null != br)
               {
                   try
                   {
                       br.close();
                   }
                   catch (IOException e)
                   {
                       e.printStackTrace();
                   }
               }
           }
           return sb.toString();
       }

经验证参数正常。

方向二:验证body参数转相应对象参数是否正确。

经跟踪确认,代码使用com.alibaba.fastjson.JSON.parseObject这个方法将body参数转成对应的对象,于是拿到对应版本的Jar包,进行测试。

获取jar包版本:

参数含有下划线开头无法正确传参问题_第7张图片

 进行测试:

package testDay01;

import java.util.Arrays;
import java.util.Date;

public class ttq {
	private String oid;

    private String docName;

    private String villageOid;

    private Date examDate;

    private String examType;

    private String fileIds;
    private String fileUrls;

    private String[] _fileIds;
    private String[] _fileUrls;
	public String getOid() {
		return oid;
	}
	public void setOid(String oid) {
		this.oid = oid;
	}
	public String getDocName() {
		return docName;
	}
	public void setDocName(String docName) {
		this.docName = docName;
	}
	public String getVillageOid() {
		return villageOid;
	}
	public void setVillageOid(String villageOid) {
		this.villageOid = villageOid;
	}
	public Date getExamDate() {
		return examDate;
	}
	public void setExamDate(Date examDate) {
		this.examDate = examDate;
	}
	public String getExamType() {
		return examType;
	}
	public void setExamType(String examType) {
		this.examType = examType;
	}
	public String getFileIds() {
		return fileIds;
	}
	public void setFileIds(String fileIds) {
		this.fileIds = fileIds;
	}
	public String getFileUrls() {
		return fileUrls;
	}
	public void setFileUrls(String fileUrls) {
		this.fileUrls = fileUrls;
	}
	public String[] get_fileIds() {
		return _fileIds;
	}
	public void set_fileIds(String[] _fileIds) {
		this._fileIds = _fileIds;
	}
	public String[] get_fileUrls() {
		return _fileUrls;
	}
	public void set_fileUrls(String[] _fileUrls) {
		this._fileUrls = _fileUrls;
	}
	@Override
	public String toString() {
		return "ttq [fileIds=" + fileIds + ", fileUrls=" + fileUrls + ", _fileIds=" + Arrays.toString(_fileIds)
				+ ", _fileUrls=" + Arrays.toString(_fileUrls) + "]";
	}
    
    
    

}
package testDay01;

import com.alibaba.fastjson.JSON;

public class test01 {
	public static void main(String[] args) {
		String json = "{\"_fileIds\":\"202202d35174f418a5447a8d7c918b6af2156a,2022021f1534d292c34ef5bf5a74519b204525\",\r\n" + 
				"\"_fileUrls\":\"0290bf3bf9174f37a8984915d396fa94.png,Capture001.png\"}";
		System.out.println(json);
		
		ttq t = JSON.parseObject(json, ttq.class);
		System.out.println(t.toString());
		
	}
}

上述代码执行后:

{"_fileIds":"202202d35174f418a5447a8d7c918b6af2156a,2022021f1534d292c34ef5bf5a74519b204525",
"_fileUrls":"0290bf3bf9174f37a8984915d396fa94.png,Capture001.png"}
ttq [fileIds=202202d35174f418a5447a8d7c918b6af2156a,2022021f1534d292c34ef5bf5a74519b204525, fileUrls=0290bf3bf9174f37a8984915d396fa94.png,Capture001.png, _fileIds=null, _fileUrls=null]
 

如果去点对象的fileIds属性时:

参数含有下划线开头无法正确传参问题_第8张图片

 此时执行程序会抛出异常,结果如下:

参数含有下划线开头无法正确传参问题_第9张图片

 如果将fileIds去掉,保留_fileIds,并将类型转为String类型,此时_fileIds可以正常传参。

如果同时拥有fileIds和_fileIds,并且设置都是String,那么此时仍然只有fileIds可以正常赋值。

 运行结果:

{"_fileIds":"202202d35174f418a5447a8d7c918b6af2156a,2022021f1534d292c34ef5bf5a74519b204525",
"_fileUrls":"0290bf3bf9174f37a8984915d396fa94.png,Capture001.png"}
ttq [_fileIds=null, fileIds=202202d35174f418a5447a8d7c918b6af2156a,2022021f1534d292c34ef5bf5a74519b204525, fileUrls=0290bf3bf9174f37a8984915d396fa94.png,Capture001.png, _fileUrls=null]
 

原因:

       通过上面的分析可以得到:com.alibaba.fastjson.JSON.parseObject(仅限fastjson-1.2.56.jar版本,其他版本未测试,结果未知)进行json转对象时只有当后端对象只有带前缀下划线的属性时,前端json带的下划线属性才能正确注入到后端对应的带下划线的对象属性,否则如果后端对象同时存在不带下划线和带下划线的属性,那么前端带下划线属性的json只能注入到后端不带下划线的属性里面。

你可能感兴趣的:(已解决问题,Java,json,java,后端,eclipse)