前景:公司有银行的项目要进行压测,但是接口有近视RSA加密,需发送签名,只能使用java编写原生接口脚本打包成jar使用BeanShell Sampler去调用发送请求。在使用的过程中遇到了如下问题。

刚开始和开发一起解决,因开发未接触Jmeter及Jmeter报错信息提示有限,极其不情愿苦逼的帮我看了下之后潇洒离去,只能自力更生,在Jmeter坑比有限的报错信息下,花了一星期才解决,所以将此问题记录。(归根结底还是自己的基础不好)

Typed variable declaration : Class: JSONObject not found in namespace

jmeter Typed variable declaration : Command not found: sendPost( com.alibaba.fastjson.JSONObject, java.lan

Typed variable declaration : Error in method invocation: Static method fromObject ( java.lang.String ) not found in class'org.json.JSONObject'

遇到的两个问题:

1.一直报没找到包的错误。嗯哼!一星期里,我把所有导包方式4种都找到了(保证万无一失)

  1)将需要的包导入目录下D:\apache-jmeter-3.3\lib\ext

  2)将需要的包导入目录下D:\apache-jmeter-3.3\bin

  3)在测试计划种添加此jar包

  4)在BeanShell Sampler里import

2一个坑,类是私有的,需改为public

3(以下两个是提醒,基本楼上两个就可以解决。)避免有空函数,注意空格。

4避免第一点路径有老旧的jar包。在ecplise中的httpclient  jar包版本要于jmeter中的版本一直或是比jmeter中的高,否则会报错java.lang.NoSuchFieldError: INSTANCE。

第一个看的是这个狗*的博客https://www.jianshu.com/p/240e929323a6,并没有相关导包的操作(深恶痛绝,妈个鸡)

下面是博主解决此问题看的博客

https://bbs.csdn.net/topics/392183602

https://blog.csdn.net/chyo098/article/details/82151019

https://blog.csdn.net/qq_30353203/article/details/68922508?locationNum=15&fps=1

https://blog.csdn.net/itest_2016/article/details/81901361(这个有多种方法)

下面这个还不错。不建议看。还未看留着慢慢看(太长了不看)。

https://gitbook.cn/books/5a8022e1e2f8ea02e7740d99/index.html?d=1534830197785

https://www.cnblogs.com/qiaoyeye/p/6953099.html

以下是源代码

package com.example.demo;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.utils.HttpClients;
import com.example.demo.utils.Md5SignUtils;
import com.example.demo.utils.RSAUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils; import javax.net.ssl.HttpsURLConnection;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL; public class RpTradeApiTest {
private static String merPrvKey =
"MIICdwIBADANBgkqhASCAmEwggJdAgEAAoGBALZVwMGkKqFOdJZc\n" +
"A43VjrOX4wRk+JlIZrR7hC2nV8V/RzuD3s2M4vMd\n" +
"yvlnOSiUy0txysWStWPcXXmrzWdpV+q4z6q4TRKwE3wK\n" +
"/nQwSgI5cX7KjrE3kXaAPPv3dpDcukfWHgVNv0FhP\n" +
"1RAf6PFUCRmqJlsfVWZI4RMsFsef0H839Mx9Em+mSqprEfYU/\n" +
"GieLLY5WQNu/qAg1nysoiKAcSolAWPwRxy96BJeWgkUOb\n" +
"DxGGFNKMvlbMgXinsQJBAPHmGNSgGEd9DgU3BkKbs9w+TG3NnwzDSt9IvyRKC681\n" +
"4MIVZEeQUI+NZU7D0e/wZe6C7oBzeqibbaH9JW3j7cMCQQDA9sXtUR3OyOSDj+46\n" +
"pea7z5QcbAZZmk431n8qQPmUL2CqOQnB54qUusvuxd2bTwuAGkqXdP1Iw1qb9OlD\n" +
"BeupAkAyKx6LOojOiskVm8vYlKJA00rWmwaZhwdIN7YsuI6jz/DFT9p0XSHYI+MW\n" +
"6tXnhVwek1kTmt+diF68cdwJZnQxAkEAqYzr8w9wYo8Qgy1HxU6aw/GsEaE1nyu/\n" +
"iL63c/gtlHBgvpFpqnB5yx+rKz2+78VvtHrlidlo6LOGfTygbZzZKQJBALXZxeCp\n" +
"tQYXn7M2FStjDwi+VzSsxXlLaMKtYEmsXdcKAAjeVdJlDICZR4ddQcgIkgdbFPih\n" +
"IMk41PXxV+mmGKs="; private static String cpPubKey =
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDAelq3gkHBW+DaTI3CcC67l1DAbPRqbLfYR4UZAJ9WN+Kaa65VEAca3Abq/F9V3wIDAQAB"; public static void trade() throws Exception {
JSONObject param = new JSONObject();
param.put("amount",(BigDecimal.valueOf(1L).setScale(2)));
param.put("mchId","000010001000000003");
param.put("outTradeNo",System.currentTimeMillis() + "");
param.put("agentType","00");
param.put("paySysBankCode","110312312315");
param.put("bankCardHolder","Jmeter代收测试");
param.put("bankCardNo","6227008888888990000");
param.put("bankName","中国银行");
param.put("bankCode","9021");
param.put("summary","扣费");
// param.put("notifyUrl","http://www.baidu.com");
//param.put("province","广东");
//param.put("city","深圳");
JSONObject result = sendPost(param, "http://192.168.1.1:8888/single.html");
System.out.println(param.toJSONString(result));
} private static void query() throws Exception {
JSONObject param = new JSONObject();
param.put("agentType","00");
param.put("mchId","000010001000000003");
param.put("outTradeNo","1541731591962");
JSONObject result = sendPost(param, "http://192.168.3.51:8888/query.html");
System.out.println(param.toJSONString(result));
} private static JSONObject sendPost(JSONObject param, String url) throws Exception {
String plainText = Md5SignUtils.createSignPlainText(param);
System.out.println("signPlain:" + plainText);
String sign = RSAUtils.sign(plainText.getBytes("UTF-8"), merPrvKey, "SHA1WithRSA");//.replaceAll("\n" , "").replaceAll("\r" , "");
System.out.println("sign:" + sign);
param.put("sign",sign);
org.apache.http.client.HttpClient httpClient = null;
org.apache.http.HttpResponse resp = null;
JSONObject result;
try {
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
String requestContent = param.toJSONString();
HttpEntity postEntity = new StringEntity(requestContent, "utf-8");
httpPost.setEntity(postEntity);
httpClient = HttpClients.createAuthNonHttpClient();
resp = httpClient.execute(httpPost);
HttpEntity entity = resp.getEntity();
String resultContent = EntityUtils.toString(entity);
System.out.println("返回结果:" + resultContent); result = JSONObject.parseObject(resultContent);
if ("0".equals(result.getString("returnCode"))) {
String verityPlainText = Md5SignUtils.createSignPlainText(result);
if (!RSAUtils.verify(verityPlainText.getBytes("UTF-8"), cpPubKey, result.getString("sign"), "SHA1WithRSA")) {
System.out.println("验证签名失败");
}
}
} finally {
HttpClientUtils.closeQuietly(resp);
HttpClientUtils.closeQuietly(httpClient);
}
return result;
}
/*
public static void main(String[] args) throws Exception {
//trade();
// query();
JSONObject param = new JSONObject();
param.put("amount",(BigDecimal.valueOf(3000000L).setScale(2)));
param.put("mchId","000010001000000003");
param.put("outTradeNo",System.currentTimeMillis() + "");
param.put("agentType","00");
param.put("paySysBankCode","110312312315");
param.put("bankCardHolder","测试");
param.put("bankCardNo","6227008888888990000");
param.put("bankName","中国银行");
param.put("bankCode","9021");
param.put("summary","扣费");
// param.put("notifyUrl","http://www.baidu.com");
//param.put("province","广东");
//param.put("city","深圳");
JSONObject result = sendPost(param, "http://192.168.3.51:8888/single.html");
System.out.println(param.toJSONString(result));
//trade; }
*/
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>demo</name>
<description>Demo project</description> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.25</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.4</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- ${project.build.directory}就是我们通常看到的target目录 -->
<outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
<excludeTransitive>false</excludeTransitive>
<stripVersion>false</stripVersion>
<!-- 不复制scope=provided的依赖包 -->
<excludeScope>provided</excludeScope>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.example.demo.RpTradeApiTest</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<!--<useUniqueVersions>false</useUniqueVersions>-->
</manifest>
</archive>
<classesDirectory>
</classesDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build> </project>

Maven pom

Jmeter not found in class'org.json.JSONObject 问题的更多相关文章

  1. Jmeter之Beanshell---使用Java处理JSON块

    原文出处:https://www.cnblogs.com/xpp142857/p/7374281.html [环境] ①Jmeter版本:3.1,JDK:1.7 ②前置条件:将json.jar包置于. ...

  2. 使用JSONObject遇到的问题,java.lang.NoClassDefFoundError: net/sf/json/JSONObject

    先是报 java.lang.NoClassDefFoundError: net/sf/json/JSONObject 这个错误, 打开项目属性找到java build path中的libaries,找 ...

  3. jsonobject 遍历 org.json.JSONObject

    import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public static  ...

  4. net.sf.json.JSONObject 和org.json.JSONObject 的差别

    http://my.oschina.net/wangwu91/blog/340721 net.sf.json.JSONObject 和org.json.JSONObject  的差别. 一.创建jso ...

  5. org.json.JSONObject的getString和optString使用注意事项

    结论:org.json.JSONObject的getString如果取不到对应的key会抛出异常,optString则不会 /** * Returns the value mapped by {@co ...

  6. Sqoop异常:Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject

    18/12/07 01:09:03 INFO mapreduce.ImportJobBase: Beginning import of staffException in thread "m ...

  7. net.sf.json.JSONOBJECT.fromObject 与 com.alibaba.fastjson.JSONObject.parseObject

    文章待补充,先写写以下知识点好了. NULL值处理之 net.sf.json.JSONObject 和 com.alibaba.fastjson.JSONObject区别 JSON作为一个轻量级的文本 ...

  8. sqoop执行job报错(org/json/JSONObject)

    NoClassDefFoundError: org/json/JSONObject: [root@hadoop2 ~]# sqoop job --create myjob7 --  import -- ...

  9. org.json.JSONObject and no properties discovered 错误解决

    自己在搭建SSM框架的时候(Spring + spring mvc + mybatis)报错内容如下: No serializer found for class org.json.JSONObjec ...

随机推荐

  1. windows服务autofac注入quartz任务

    一.nuget下载相关类库引用 install-package Quartz install-package Autofac install-package Autofac.Configuration ...

  2. P1073 最优贸易 建立分层图 + spfa

    P1073 最优贸易:https://www.luogu.org/problemnew/show/P1073 题意: 有n个城市,每个城市对A商品有不同的定价,问从1号城市走到n号城市可以最多赚多少差 ...

  3. codeforces 828 C. String Reconstruction(思维+优先队列)

    题目链接:http://codeforces.com/contest/828/problem/C 题解:有点意思的题目,可用优先队列解决一下具体看代码理解.或者用并查集或者用线段树都行. #inclu ...

  4. 百度之星资格赛 调查问卷 bitset模板(直接将字符串转化成二进制数组并可以计算出十进制值)

    Problem Description 度度熊为了完成毕业论文,需要收集一些数据来支撑他的论据,于是设计了一份包含 mm 个问题的调查问卷,每个问题只有 'A' 和 'B' 两种选项. 将问卷散发出去 ...

  5. 【Offer】[50-2] 【字符流中第一个只出现一次的字符】

    题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次 ...

  6. c语言实现去除字符串首尾空格

    字符串内存图如下: 引入头文件: 1 #include<stdlib.h> 2 #include<stdio.h> 3 #include<string.h> 函数原 ...

  7. 集成学习方法Boosting和Bagging

    集成学习是通过构架并结合多个学习器来处理学习任务的一种思想, 目前主要分为两大类:Boosting和Bagging. 对于任意一种集成方法, 我们都希望学习出来的基分类器具有较高的准确性和多样性, 基 ...

  8. length属性、length()方法和size()的方法的区别

    JAVA 1. length属性是针对Java中的数组来说的,要求数组的长度可以用其length属性: 2.length()方法是针对字符串来说的,要求一个字符串的长度就要用到它的length()方法 ...

  9. C# 表达式树分页扩展(三)

    一.前言 前面我们知道了表达树的基本知识,也明白了怎么遍历和修改一个表达式,这里我们就一个实际的场景来进行功能开发. 表达式系列目录 C# 表达式树讲解(一) C# 表达式树遍历(二) C# 表达式树 ...

  10. 解决The mysql extension is deprecated and will be removed

    今天打开网站突然发现报错:“PHP Deprecated:  mysql_connect(): The mysql extension is deprecated and will be remove ...