gpt4 book ai didi

spring - 如何使用spring来编码和解码xml?

转载 作者:IT老高 更新时间:2023-10-28 13:58:28 26 4
gpt4 key购买 nike

我有一个 Spring Boot 项目。我的项目中有一些 xsd。我已经使用 maven-jaxb2-plugin 生成了这些类。我用过this让示例 Spring Boot 应用程序运行的教程。

import org.kaushik.xsds.XOBJECT;

@SpringBootApplication
public class JaxbExample2Application {

public static void main(String[] args) {
//SpringApplication.run(JaxbExample2Application.class, args);
XOBJECT xObject = new XOBJECT('a',1,2);

try {
JAXBContext jc = JAXBContext.newInstance(User.class);

Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(xObject, System.out);

} catch (PropertyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

但我担心的是我需要映射架构的所有 jaxb 类。 Spring 中还有什么东西可以用来让我的任务更轻松。我看过 Spring OXM项目,但它在 xml 中配置了应用程序上下文。 Spring Boot 有什么我可以开箱即用的东西吗?任何示例都会有所帮助。

编辑

我试过 xerx593's answer我使用 main 方法进行了一个简单的测试

    JaxbHelper jaxbHelper = new JaxbHelper();
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(XOBJECT.class);
jaxbHelper.setMarshaller(marshaller);
XOBJECT xOBJECT= (PurchaseOrder)jaxbHelper.load(new StreamSource(new FileInputStream("src/main/resources/PurchaseOrder.xml")));
System.out.println(xOBJECT.getShipTo().getName());

它运行得非常好。现在我只需要使用 Spring Boot 将其插入即可。

最佳答案

OXM 绝对适合您!

Jaxb2Marshaller 的简单 java 配置如下所示:

//...
import java.util.HashMap;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
//...

@Configuration
public class MyConfigClass {
@Bean
public Jaxb2Marshaller jaxb2Marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(new Class[]{
//all the classes the context needs to know about
org.kaushik.xsds.All.class,
org.kaushik.xsds.Of.class,
org.kaushik.xsds.Your.class,
org.kaushik.xsds.Classes.class
});
// "alternative/additiona - ly":
// marshaller.setContextPath(<jaxb.context-file>)
// marshaller.setPackagesToScan({"com.foo", "com.baz", "com.bar"});

marshaller.setMarshallerProperties(new HashMap<String, Object>() {{
put(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, true);
// set more properties here...
}});

return marshaller;
}
}

在您的应用程序/服务类中,您可以这样处理:

import java.io.InputStream;
import java.io.StringWriter;
import javax.xml.bind.JAXBException;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;

@Component
public class MyMarshallerWrapper {
// you would rather:
@Autowired
private Jaxb2Marshaller marshaller;
// than:
// JAXBContext jc = JAXBContext.newInstance(User.class);
// Marshaller marshaller = jc.createMarshaller();

// marshalls one object (of your bound classes) into a String.
public <T> String marshallXml(final T obj) throws JAXBException {
StringWriter sw = new StringWriter();
Result result = new StreamResult(sw);
marshaller.marshal(obj, result);
return sw.toString();
}

// (tries to) unmarshall(s) an InputStream to the desired object.
@SuppressWarnings("unchecked")
public <T> T unmarshallXml(final InputStream xml) throws JAXBException {
return (T) marshaller.unmarshal(new StreamSource(xml));
}
}

Jaxb2Marshaller-javadoc ,以及相关的 Answer

关于spring - 如何使用spring来编码和解码xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44676532/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com