【问题标题】:@RequestBody null can't understand why SpringBoot@RequestBody null 无法理解为什么 SpringBoot
【发布时间】:2021-11-21 02:00:01
【问题描述】:

当我尝试发送定义的 XML 时,我真的不明白为什么我的 @RequestBody 为空。

这里是我尝试推送的 XML

    <File>
        <FileDate>2020-12-25</FileDate>
        <RecordCount>2</RecordCount>
        <transactionMTDs>
            <transactionMTD>
                <RecordID>45</RecordID>
            </transactionMTD>
        </transactionMTDs>
    </File>

我的实体文件

package com.entities;

import javax.xml.bind.annotation.*;
import java.io.Serializable;
import java.util.List;

@XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
            "fileDate",
            "recordCount",
            "transactions"
    })
    @XmlRootElement(name = "File")
    public class File {
        @XmlElement(name = "FileDate", required = true)
        protected String fileDate;
        @XmlElement(name = "RecordCount", required = true)
        protected String recordCount;

        @XmlElementWrapper(name="transactionMTDs")
        @XmlElements({
            @XmlElement(name="transactionMTD",     type=Transaction.class),
        })
        protected List<Transaction> transactions;

        public String getFileDate() {
            return fileDate;
        }

        public void setFileDate(String fileDate) {
            this.fileDate = fileDate;
        }

        public String getRecordCount() {
            return recordCount;
        }

        public void setRecordCount(String recordCount) {
            this.recordCount = recordCount;
        }

        public List<Transaction> getTransaction() {
            return transactions;
        }

        public void setTransaction(List<Transaction> transaction) {
            this.transactions = transaction;
        }
    }

我的实体交易

package com.entities;

import javax.xml.bind.annotation.*;
import java.io.Serializable;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
        "recordID"
})
@XmlRootElement(name = "transactionMTD")
public class Transaction {
    @XmlElement(name = "RecordID", required = true)
    protected String recordID;
// setter and getter


    public String getRecordID() {
        return recordID;
    }

    public void setRecordID(String recordID) {
        this.recordID = recordID;
    }

}

我的控制器

package com.controller;
import com.entities.File;
import com.mq.AMQPConfig;
import com.mq.RabbitMQProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.xml.bind.JAXB;
import java.io.StringWriter;

/*
example to be tested and worked
    <File>
        <FileDate>2020-12-25</FileDate>
        <RecordCount>2</RecordCount>
        <transactionMTDs>
            <transactionMTD>
                <RecordID>45</RecordID>
            </transactionMTD>
        </transactionMTDs>
    </File>
*/
    @ComponentScan({"org.springframework.amqp.rabbit","com.mq","com.entities"})
    @RestController
    public class FileController {

        @Autowired
        private AMQPConfig amqpconfig;
        @Autowired
        private RabbitTemplate rabbitTemplate;

        @Autowired
        RabbitMQProperties rabbitMQProperties;


        @RequestMapping(method = RequestMethod.POST, consumes = {MediaType.APPLICATION_XML_VALUE})
        public void parseXML(@RequestBody File file) {

        }
        @PostMapping("/save-file")
        public void pushXML(@RequestBody File file) {
            System.out.println(file.getFileDate());
            rabbitTemplate.convertAndSend(rabbitMQProperties.getExchange(), rabbitMQProperties.getRoutingKey(), file);

        }

        @GetMapping("/trest")
        public void trest() {
            System.out.println("ok");
            rabbitTemplate.convertAndSend(rabbitMQProperties.getExchange(), rabbitMQProperties.getRoutingKey(),"test");

        }
    }

还有我的 pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <jackson.version>2.12.5</jackson.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</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-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-oauth2-client</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-config -->
        <!--<dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
        </dependency>-->
        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.6</version>
        </dependency>

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

        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>5.13.1</version>
        </dependency>


        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>${jackson.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>com.fasterxml.jackson.core</groupId>
                    <artifactId>jackson-dataformat-xml</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

我想这与排除有关,但我不太确定......

非常感谢您的帮助

尼古拉斯

【问题讨论】:

    标签: java spring spring-boot maven spring-mvc


    【解决方案1】:

    所以有了xml:

    <file>
            <fileDate>2020-12-25</fileDate>
            <recordCount>2</recordCount>
            <transactions>
                <transaction>
                    <recordId>45</recordId>
                </transaction>
            </transactions>
        </file>
    

    还有以下对象:

    package com.entities;
    
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlElementWrapper;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlType;
    import java.util.List;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "fileDate",
        "recordCount",
        "transactions"
    })
    @XmlRootElement(name = "file")
    public class File {
      @XmlElement(name = "fileDate", required = true)
      protected String fileDate;
      @XmlElement(name = "recordCount", required = true)
      protected String recordCount;
    
      @XmlElementWrapper(name="transactions")
      @XmlElement(name="transaction")
      protected List<Transaction> transactions;
    
      public String getFileDate() {
        return fileDate;
      }
    
      public void setFileDate(String fileDate) {
        this.fileDate = fileDate;
      }
    
      public String getRecordCount() {
        return recordCount;
      }
    
      public void setRecordCount(String recordCount) {
        this.recordCount = recordCount;
      }
    
      public List<Transaction> getTransactions() {
        return transactions;
      }
    
      public void setTransactions(List<Transaction> transactions) {
        this.transactions = transactions;
    
    
         }
        }
    
    package com.entities;
    
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlType;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "recordId"
    })
    @XmlRootElement(name = "transaction")
    public class Transaction {
      @XmlElement(name = "recordId", required = true)
      protected String recordId;
    
      public String getRecordId() {
        return recordId;
      }
    
      public void setRecordId(String recordId) {
        this.recordId = recordId;
      }
    }
    

    使用您的控制器对 xml 进行反序列化。

    【讨论】:

      猜你喜欢
      • 2021-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      • 2022-12-12
      相关资源
      最近更新 更多