0

I'm currently developing a travel service and we have to write user information to an XML file using JAXB Marshalling. When running the app on the Tomcat server, I type in the information in the textbox and click "Register", the XML format is written within the console, but it's not actually written to the XML file. As of right now, I have my "accounts.xml" in the "src/main/resources" directory. Here is my controller information so far. I'm not exactly sure why it won't write to the XML file:

@Controller
public class AccountController {
    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String registerPage(Locale locale, Model model) {
        return "register";
    }

    @RequestMapping(value = "/home", method = RequestMethod.POST)
    public String register(@RequestParam("name") String name, @Validated User user, Model model) throws JAXBException, IOException{

        Account account = new Account();
        account.setName(name);
        account.setEmail("email");
        account.setPassword("password");

        try {


            File file = ResourceUtils.getFile("classpath:accounts.xml");


            JAXBContext jaxbContext = JAXBContext.newInstance(Account.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            jaxbMarshaller.marshal(account, file);
            jaxbMarshaller.marshal(account, System.out);


              } catch (JAXBException e) {
                  e.printStackTrace();
              }


        model.addAttribute("name", user.getName());
        return "user";
    }


}

My console output looks like this when I submit the information:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<account>
    <email>email</email>
    <name>testName</name>
    <password>password</password>
</account>

Any help would be appreciated!

kdarling
  • 59
  • 2
  • Hi, if any of the answers solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – areus Mar 05 '20 at 13:57

2 Answers2

0

File file = ResourceUtils.getFile("classpath:accounts.xml"); is a "java.io.File" object which references a file accounts.xml part of the classpath. This is part of the war file being deployed in your tomcat.

You need to use a File object as in File file = new File("/tmp/accounts.xml")

GerritCap
  • 1,606
  • 10
  • 9
0

If you really want to do that, you should make sure that your WAR is deployed exploded, and look if accounts.xml is writable. Have at look at this: How can I save a file to the class path

Besides, I see that you create a JAXBContext in every request. The JAXB spec says:

To avoid the overhead involved in creating a JAXBContext instance, a JAXB application is encouraged to reuse a JAXBContext instance. An implementation of abstract class JAXBContext is required to be thread-safe, thus, multiple threads in an application can share the same JAXBContext instance.

You should move JAXBContext to an static field, as only one instance is needed.

areus
  • 2,880
  • 2
  • 7
  • 17