代码之家  ›  专栏  ›  技术社区  ›  Nuñito Calzada

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

  •  0
  • Nuñito Calzada  · 技术社区  · 6 年前

    我有一个基本的springboot2.1.5.RELEASE应用程序。使用Spring初始值设定项、JPA、嵌入式Tomcat、Thymeleaf模板引擎,并将包作为带有一些rest控制器的可执行JAR文件。

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

    @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();
    
        }
    

    java.lang.NoSuchMethodError: org.json.JSONObject.<init>(Ljava/lang/Object;)V
        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>
    
    0 回复  |  直到 6 年前
        1
  •  10
  •   Shubham Kadlag    6 年前

    看起来你有不止一个组织。json:json dependency 在你的课堂上。

    看看它:

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

    com.jayway.jsonpath:json-pathwhich

    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路径中可能有依赖于旧json库的内容的内容,而新版本中不再有这些内容,因此请谨慎行事,彻底测试所有内容。

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

        2
  •  2
  •   rreay724    5 年前

    我在另一页找到这个。我尝试了检查答案在这里,它没有与我的工作,但当我插入到我的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>
    
        3
  •  0
  •   Yogesh Patil    6 年前

    你的 destinationAddress refundAddress HotelswitchHotelOrderRequestBody 你的 目的地地址 退款地址 被称为单个地址元素。请更新pojo或JSON,然后重试。这可能是个问题。

        4
  •  0
  •   Ori Marko    6 年前

    json dependency

    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20180130</version>
    </dependency>
    
        5
  •  0
  •   Hưng Chu    6 年前

    问题应该是destinationAddress和ReturnAddress在JSON和POJO之间不匹配。 尝试这样更改JSON:

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

        6
  •  0
  •   Toerktumlare    6 年前

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

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

    ObjectMapper objectMapper = new ObjectMapper();
    String json = objectMapper.writeValueAsString(car)
    
        7
  •  0
  •   venugopal    4 年前

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

    从springbootstarter测试中排除androidjson对我很有效。

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