简体   繁体   中英

Problem with accessing xml in Spring boot resource folder

Hi everyone I have a question. I am running a Spring Boot application, where I have DataInitializer that implements the CommandLineRunner and Override run method, to simulate starter of the app. The DataInitializer is annotated as @Component. The project uses Maven and I added three dependencies for 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>

The problem is that in this DataInitializer run method, I am initialising and object called 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;
    }

.......getters and setters

It is using the jaxb annotations to be used as mapper. When I initialise this OBJECT and create an instance, I am trying to write the marshalled data into the resource folder, where there is already created xml. Here is the code:

@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"));


    }
}

I am using IntliJ Ultimate Edition, so when I go to the users.xml file in the resource folder and click copy path - > I'm choosing path from content root . The problem is when I run the application and this run method is called by the JVM, I'm receiving error and the application stops. Error:

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

When is use the absolute path, everything works. I now when the application is compiled the folders doesn't work like when you write code. The main reason for this problem is that on a windows machine, again with InteliJ Ultimate Edition, everything works with path from content root . For me I can use only the absolute, or choose file name and create it outside from the src folder. I am on MacOS machine. Is there some settings to the intelij or anything, because it is strange how on windows works but for me not. The windows machine is not mine so I don't know the settings and at the moment I don't have contact with the person. We are from a Spring Data course and see him only on the lectures. When I print the file.exists(), it tells me that the file exist -> true.

Edit: I tried with src/main/resources/xmls/users.xml, xmls/users.xml, resources/xmls/users.xml and etc. only the absolute path manages to access this files.

The problem was with the InteliJ, I ruined it on InteliJ EAP version 2020.1 and everything works fine. If some one has this problem you can just try with other version or reset all settings of your one. Or just re-install the IDE. Thanks to all of you for the answers. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM