Jmeter not found in class'org.json.JSONObject 问题

 

 

前景:公司有银行的项目要进行压测,但是接口有近视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;

    }
*/
}
View Code

 

<?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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2018-12-11 15:44  Zing_Z  阅读(7032)  评论(0编辑  收藏  举报

导航