spring 从Java Object创建XML

ezykj2lf  于 4个月前  发布在  Spring
关注(0)|答案(1)|浏览(42)

我尝试从Java对象创建XML,但出现错误“在模块路径或类路径上未找到JAXB-API的实现”。请帮助我解决错误。

@RequestMapping(value = "/writeToFile",method = RequestMethod.POST)
    public String writeFileXML(@RequestBody Customer customer) {
        try {
            File file=new File("D:\\vendorfile\\customer.xml");
            FileWriter fileWriter=new FileWriter(file);
            JAXBContext jaxbContext=JAXBContext.newInstance(Customer.class);
            Marshaller marshaller=jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.marshal(customer, fileWriter);
            return "Marshaller successfull";
        }catch(IOException ioException) {
            return ioException.getMessage();
        }catch(Exception ex) {
            return ex.getMessage();
        }
    }

个字符

vmdwslir

vmdwslir1#

您缺少了JAXB-API依赖项(因为您只定义了JAXB-API的依赖项,并且正如您得到的错误消息所指出的那样)。
从JDK 9开始,与jaxb相关的类不再是JDK的一部分
只需添加以下内容即可解决错误

<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>2.3.9</version>
</dependency>

字符串

相关问题