java - Spring boot资源文件夹中访问xml的问题

标签 java xml spring-boot spring-data-jpa jaxb

大家好,我有一个问题。我正在运行一个 Spring Boot 应用程序,其中有 DataInitializer 实现 CommandLineRunner 和 Override run 方法,以模拟应用程序的启动程序。 DataInitializer 被注释为@Component。该项目使用 Maven,我为 jaxb 添加了三个依赖项:

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
        </dependency>

问题是在这个 DataInitializer run 方法中,我正在初始化一个名为 UserDto 的对象:

@XmlRootElement(name = "user")
@XmlAccessorType(value = XmlAccessType.FIELD)
public class UserDto {

    @XmlElement(name = "first_name")
    private String firstName;

    @XmlElement(name = "last_name")
    private String lastName;

    @XmlElement(name = "city")
    private String city;

    public UserDto() {
    }

    public UserDto(String firstName, String lastName, String city) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.city = city;
    }

......getter 和 setter

它使用 jaxb 注释用作映射器。当我初始化这个对象并创建一个实例时,我尝试将编码数据写入资源文件夹,其中已经创建了 xml。这是代码:

@Component
public class DataInitializer implements CommandLineRunner {

    private final UserService userService;


    @Autowired
    public DataInitializer(UserService userService) {
        this.userService = userService;

    }
    private final String USERS_FILE_PATH = "src/main/resources/xmls/users.xml";

    @Override
    public void run(String... args) throws Exception {

        UserDto user = new UserDto("k", "d", "e");


        JAXBContext context = JAXBContext.newInstance(UserDto.class);


        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);


        marshaller.marshal(user, new File("src/main/resources"));


    }
}

我使用的是 IntliJ Ultimate Edition,因此当我转到资源文件夹中的 users.xml 文件并单击复制路径 -> 我选择来自内容根目录的路径。问题是当我运行应用程序并且 JVM 调用此运行方法时,我收到错误并且应用程序停止。错误:

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:787) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:768) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at com.softuni.xmlmapping.XmlMappingApplication.main(XmlMappingApplication.java:12) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.2.5.RELEASE.jar:2.2.5.RELEASE]
Caused by: javax.xml.bind.JAXBException: null
    at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:123) ~[jaxb-api-2.3.0.jar:2.3.0]
    at com.softuni.xmlmapping.init.DataInitializer.run(DataInitializer.java:51) ~[classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:784) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    ... 10 common frames omitted
Caused by: java.io.FileNotFoundException: src/main/resources (Is a directory)
    at java.base/java.io.FileOutputStream.open0(Native Method) ~[na:na]
    at java.base/java.io.FileOutputStream.open(FileOutputStream.java:292) ~[na:na]
    at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:235) ~[na:na]
    at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:185) ~[na:na]
    at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:116) ~[jaxb-api-2.3.0.jar:2.3.0]
    ... 12 common frames omitted

当使用绝对路径时,一切正常。现在,当编译应用程序时,文件夹不像编写代码时那样工作。此问题的主要原因是,在 Windows 计算机上,再次使用 InteliJ Ultimate Edition,一切都可以使用来自内容根的路径。对我来说,我只能使用绝对路径,或者选择文件名并在 src 文件夹外部创建它。我在 MacOS 机器上。 intelij 是否有一些设置或其他设置,因为在 Windows 上的工作方式很奇怪,但对我来说不是。 Windows 机器不是我的,所以我不知道设置,目前我没有与该人联系。我们来自 Spring Data 类(class),只在讲座中见到他。当我打印 file.exists() 时,它告诉我文件存在 -> true。

编辑:我尝试使用 src/main/resources/xmls/users.xml、xmls/users.xml、resources/xmls/users.xml 等。只有绝对路径才能访问这些文件。

最佳答案

问题出在 InteliJ 上,我在 InteliJ EAP 版本 2020.1 上毁掉了它,一切正常。如果有人遇到此问题,您可以尝试使用其他版本或重置您的版本的所有设置。或者直接重新安装IDE。感谢大家的回答。 :)

关于java - Spring boot资源文件夹中访问xml的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60770557/

相关文章:

java - Spring @Autowired 实例为 null

php - 响应XML包含 “2000 ”和 “20a0”字符

sql-server - T-SQL 查找具有匹配文本的节点并从 XML 字段中的同级节点中提取值

javascript - 需要使用jquery提取XML节点,其中节点的sibling等于某个值;现在使用 xPath

java - 如何使用 spring-boot 显示 Dropwizard Metrics Servlet?

java - 在后端(java spring boot)排序中,如何使用可分页的别名进行排序?没有父表作为前缀

java - 将 setVisible() 函数放在函数的开头是否与我将它放在该函数的末尾有所不同?

java - 变量只能为null显示eclipse

java - Apache POI 3.6 : Reading an xlsx file

java.lang.IllegalArgumentException : Not a managed type - Multiple Databases in Spring Boot