java.lang.NoSuchMethodError: org.json.JSONObject.<init>(Ljava/lang/Object;)V

Posted

技术标签:

【中文标题】java.lang.NoSuchMethodError: org.json.JSONObject.<init>(Ljava/lang/Object;)V【英文标题】: 【发布时间】:2019-10-17 17:31:23 【问题描述】:

我有一个基本的 SpringBoot 2.1.5.RELEASE 应用程序。使用 Spring Initializer、JPA、嵌入式 Tomcat、Thymeleaf 模板引擎,并打包为带有一些 RestControllers 的可执行 JAR 文件。

在控制器的 1 中,这是我发送的主体:


    "depositHotel": "xxx",
    "destinationHotel": "aaa",
    "depositHotelAmount": "0.2",
    "destinationHotelAmount": "4",
    "destinationAddress": [
        "address": "asdf",
        "tag": ""
    ],
    "refundAddress": [
        "address": "pio",
        "tag": ""
    ]

所以我创建了这个类以将其作为 RequestBody 发送:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder(
"address",
"tag"
)
public class Address 



    public Address() 
        super();
    

    public Address(String address) 
        super();
        this.address = address;
    

    @JsonProperty("address")
    private String address;
    @JsonProperty("tag")
    private Object tag;


    @JsonProperty("address")
    public String getAddress() 
    return address;
    

    @JsonProperty("address")
    public void setAddress(String address) 
    this.address = address;
    

    @JsonProperty("tag")
    public Object getTag() 
    return tag;
    

    @JsonProperty("tag")
    public void setTag(Object tag) 
    this.tag = tag;
    

公共类 HotelswitchHotelOrderRequestBody

    public static class Builder 

        private String depositHotel;
        private String destinationHotel;
        private Float depositHotelAmount;
        private Float destinationHotelAmount;
        private Address destinationAddress;
        private Address refundAddress;


        public Builder(String depositHotel, String destinationHotel) 
            this.depositHotel = depositHotel;
            this.destinationHotel = destinationHotel;
        

        public Builder withDepositHotelAmount (Float depositHotelAmount) 
            this.depositHotelAmount = depositHotelAmount;
            return this;  
        

        public Builder withDestinationHotelAmount (Float destinationHotelAmount) 
            this.destinationHotelAmount = destinationHotelAmount;
            return this;  
        

        public Builder toDestinationAddress (Address destinationAddress) 
            this.destinationAddress = destinationAddress;
            return this;  
        

        public Builder toRefundAddress (Address refundAddress) 
            this.refundAddress = refundAddress;
            return this;  
        

        public HotelswitchHotelOrderRequestBody build()

            HotelswitchHotelOrderRequestBody order = new HotelswitchHotelOrderRequestBody(); 
            order.depositHotel = this.depositHotel;
            order.depositHotelAmount = this.depositHotelAmount;
            order.destinationAddress = this.destinationAddress;
            order.destinationHotel = this.destinationHotel;
            order.destinationHotelAmount = this.destinationHotelAmount;
            order.refundAddress = this.refundAddress;

            return order;

        
    


    private String depositHotel;
    private String destinationHotel;
    private Float depositHotelAmount;
    private Float destinationHotelAmount;
    private Address destinationAddress;
    private Address refundAddress;


    private HotelswitchHotelOrderRequestBody () 
        //Constructor is now private.
    


    public String getDepositHotel() 
        return depositHotel;
    


    public void setDepositHotel(String depositHotel) 
        this.depositHotel = depositHotel;
    


    public String getDestinationHotel() 
        return destinationHotel;
    


    public void setDestinationHotel(String destinationHotel) 
        this.destinationHotel = destinationHotel;
    


    public Float getDepositHotelAmount() 
        return depositHotelAmount;
    


    public void setDepositHotelAmount(Float depositHotelAmount) 
        this.depositHotelAmount = depositHotelAmount;
    


    public Float getDestinationHotelAmount() 
        return destinationHotelAmount;
    


    public void setDestinationHotelAmount(Float destinationHotelAmount) 
        this.destinationHotelAmount = destinationHotelAmount;
    


    public Address getDestinationAddress() 
        return destinationAddress;
    


    public void setDestinationAddress(Address destinationAddress) 
        this.destinationAddress = destinationAddress;
    


    public Address getRefundAddress() 
        return refundAddress;
    

    public void setRefundAddress(Address refundAddress) 
        this.refundAddress = refundAddress;
    

public test postOrder ( HotelswitchHotelOrderRequestBody order) 



        HttpEntity<String> entity = new HttpEntity<String>(new JSONObject(order).toString(), headers());

        ResponseEntity<OrderResponse> response = new RestTemplate()
                  .exchange(URL, 
                          HttpMethod.POST, entity,  new ParameterizedTypeReference<OrderResponse>() );

        return response.getBody();

    

但我有这个错误:


    at io.bonanza.backend.service.Hotelswitch.HotelswitchHotelService.postOrder(HotelswitchHotelService.java:132)
    at io.bonanza.backend.service.Hotelswitch.HotelswitchHotelServiceTests.testPostOrder(HotelswitchHotelServiceTests.java:151)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

pom.xml:

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>       

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency> 

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
         </dependency>

         <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>com.googlecode.libphonenumber</groupId>
            <artifactId>libphonenumber</artifactId>
            <version>8.9.0</version>
        </dependency>


        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
        </dependency>

        <dependency>
             <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>

        <!-- Spring Security -->

        <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-test -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>





        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
           <!--  <version>4.5.4</version> -->
        </dependency>

        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>

           <!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
       <dependency>
          <groupId>javax.ws.rs</groupId>
          <artifactId>javax.ws.rs-api</artifactId>
          <version>2.1.1</version>
       </dependency>


        <!-- Firebase dependencies -->
       <dependency>
            <groupId>com.google.firebase</groupId>
            <artifactId>firebase-admin</artifactId>
            <version>5.4.0</version>
        </dependency>

        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>google-cloud-firestore</artifactId>
            <version>0.26.0-beta</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>23.0</version>
        </dependency>

    </dependencies>

【问题讨论】:

请添加您的 POM。这通常是由于不允许 Spring 管理依赖项。它管理。 有时 pom.xml 文件 (Maven) 中的依赖项存在问题。造成这种情况的原因可能有很多:版本冲突,或者使用不同版本两次声明相同依赖项的冲突。 你能把 mvn dependency:tree 的结果贴出来吗? 您使用哪个 Java 版本来运行它? 针对这个问题,导入了多个版本的org.json。解决方案是将其从导入它的依赖项中排除,并指定预期的依赖项。此外,您可以使用fastjson,它也已导入到您的项目中。 【参考方案1】:

您的类路径似乎有多个 org.json:json 依赖项。

看着它:

org.springframework.boot:spring-boot-starter-test 取决于

com.jayway.jsonpath:json-path 又带来了

org.json:json 可能比其版本更新/旧

io.jsonwebtoken jjwt 0.9.1 依赖于

您可以尝试从 spring-boot-starter-test/io.jsonwebtoken 中排除这种传递依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
        </exclusion>
    </exclusions>
</dependency>

或/与

<dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt</artifactId>
    <version>0.9.1</version>
    <exclusions>
        <exclusion>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
        </exclusion>
    </exclusions>
</dependency>

但是 json-path 中的某些内容可能依赖于旧 json 库中的某些内容,而新版本已不再存在,因此请谨慎行事并彻底测试所有内容。 也有可能是别的东西带来了 org.json:json。

要验证,请运行mvn dependency:tree 并在生成的输出中搜索org.json:json

【讨论】:

如果回答后有任何问题或不起作用,请发表评论。如果它解决了接受答案的问题,将不胜感激。【参考方案2】:

请检查此链接: spring-boot issue 8706

从 spring-boot-starter-test 中排除 android-json 对我有用。

 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>com.vaadin.external.google</groupId>
                <artifactId>android-json</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

【讨论】:

【参考方案3】:

我在另一个页面上找到了这个。我在这里尝试了检查过的答案,但它不适用于我的,但是当我将它插入我的 pom 文件时它可以工作。我正在使用 mockito 运行单元测试,其中一个端点使用 JSONObject.put 并且给了我没有这样的方法错误。

<dependency>
        <groupId>
            org.springframework.boot
        </groupId>
        <artifactId>
            spring-boot-starter-test
        </artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.json</groupId>
                <artifactId>json</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.vaadin.external.google</groupId>
                <artifactId>android-json</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

【讨论】:

这对我有用,但我也必须在更改后刷新我的 Maven 依赖项。谢谢。 也为我工作。仅添加 org.json 排除是不够的。 (spring-boot 2.4.4 和 Junit 5)【参考方案4】:

您应该不需要 JSON 对象

HttpEntity<HotelswitchHotelOrderRequestBody> entity = new HttpEntity<HotelswitchHotelOrderRequestBody>(order, headers());

如果您只想将对象转换为字符串,请使用对象映射器。

ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(car)

【讨论】:

【参考方案5】:

问题应该是您的 JSON 和 POJO 之间的destinationAddress 和refundAddress 不匹配。 尝试像这样更改您的 JSON:

"depositHotel": "xxx", "destinationHotel": "aaa", "depositHotelAmount": "0.2", "destinationHotelAmount": "4", "destinationAddress": "address": "asdf", "tag": "" , "refundAddress": "address": "pio", "tag": ""

如果您不想更改 JSON,请将 Builder POJO 更改为使用 Address[] 而不是 Address 对象。

【讨论】:

【参考方案6】:

您需要使用 ObjectMapper 将 POJO 转换为 JSONObject 或包含 json dependency

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20180130</version>
</dependency>

【讨论】:

【参考方案7】:

您的destinationAddressrefundAddress 在JSON 中作为地址列表或地址数组传递,而在HotelswitchHotelOrderRequestBody 中您的destinationAddressrefundAddress 被称为单个地址元素。更新您的 pojo 或 JSON,然后重试。这可能是个问题。

【讨论】:

以上是关于java.lang.NoSuchMethodError: org.json.JSONObject.<init>(Ljava/lang/Object;)V的主要内容,如果未能解决你的问题,请参考以下文章