我有一个Spring Boot项目。我的项目中有几个xsds。我已经使用maven-
jaxb2-plugin生成了类。我已使用运行示例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中配置了应用程序上下文。弹簧靴有什么我可以直接使用的东西吗?任何示例都将有所帮助。
编辑
我尝试了xerx593的答案,并且使用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;
}
}
在您的Application / Service类中,您可以采用以下方式:
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));
}
}
问题内容: 我正在尝试创建一个非常简单的REST服务器。我只是有一个测试方法,它将返回字符串列表。这是代码: 它给出以下错误: 我希望JAXB对诸如String,Integer等简单类型具有默认设置。我想不是。这是我的想象: 使这种方法最简单的方法是什么? 问题答案: 我使用@LiorH的示例并将其扩展为: 注意,它使用泛型,因此您可以将其与String之外的其他类一起使用。现在,应用程序代码很简
问题内容: 我一直在尝试JAXB教程,并设法使代码能够从Java对象生成XML文件,然后能够使用XML生成Java对象。目前,它读取同一类的多个实例以创建一个类似于以下内容的XML文件 我希望能够使用JAXB技术来处理子类。例如:说我有一个Car,Van和Bicycle对象,它们是Vehicle的子类。我是否可以操纵我的JAXB类来编写一个会产生类似结果的XML文件?我在下面提供了我正在使用的代码
我必须在json中解析REST响应,它有很多带有许多对象的嵌套列表。 响应包含一个名为“ObjectList”的项目,其中有一个列表和两个元素,“ObjectA”和“ObjectB”。我不知道如何使用Jackson注释解析对对象的响应。 json看起来像这样: 我的代码如下所示 回应是: 这根本行不通,我对如何绘制这张地图感到困惑。
问题内容: 我在代码中遇到问题。 当我在本地(http:// localhost:8080 / home / rest / parameter / )上运行应用程序时,出现以下错误。我的主要要求是在xml上的UI上显示所有信息。 org.jboss.resteasy.spi.UnhandledException:com.sun.xml.bind.v2.runtime.IllegalAnnotati
问题内容: 我有一个必须符合且包含值数组的对象。我怎么能用/对其进行编码/解码?额外的问题:如何编码最紧凑?(它必须进入保存的Game Center状态数据,其大小是有限的。) 理想情况下,我只想写一个等于数组大小的an ,然后写乘a的64位乘以类似的方式读取它。我可以这样做吗? 不起作用。 问题答案: 这是将数组编码为字节数组的可能解决方案。它的灵感来自于如何使用NSCoding序列化C数组?。
问题内容: 我有一个要显示字符串数组的组件。代码看起来像这样。 运行正常。例如,如果 props.data = [‘tom’,’jason’,’chris’] 页面中呈现的结果将为 tomjasonchris 然后,我想使用逗号连接所有名称,因此我将代码更改为 但是,渲染的结果是 [Object],[Object],[Object]。 我不知道如何解释对象成为要渲染的反应组件。有什么建议吗? 问题
问题内容: 我需要对UTF-16字节数组进行编码/解码。字节数组是通过字节顺序标记(BOM)给我的,我需要使用BOM编码字节数组。 另外,由于我正在与Microsoft客户端/服务器打交道,因此我希望以小字节序(与LE BOM一起)发出编码,以避免任何误解。我确实意识到,使用BOM可以在大端模式下工作,但是我不想在Windows世界中游走。 例如,以下是一种使用BOM 编码为little endi
我正在这样使用spring boot和rest模板。 在上面的代码中,我得到了字符串形式的响应。我想将每个值映射到一个pojo类,并将其插入到我的数据库中。现在我还不知道如何将这些数组映射到pojo类中。