我正在编写一个 Spring Web 应用程序,并且希望将我的对象转换为没有注释的 xml。我知道使用 xml 配置你可以这样做:
<bean id="jaxb2Marshaller" class="software.bytepushers.userprofile.models.JaxbIntroductionsMarshaller">
<property name="classesToBeBound">
<list>
<value>software.bytepushers.userprofile.models.Person</value>
<value>software.bytepushers.userprofile.models.WebServiceResponse</value>
</list>
</property>
<property name="jaxbContextProperties">
<map>
<entry>
<key>
<util:constant static-field="com.sun.xml.bind.api.JAXBRIContext.ANNOTATION_READER"/>
</key>
<bean class="org.jboss.jaxb.intros.IntroductionsAnnotationReader">
<constructor-arg ref="jaxbIntroductions"/>
</bean>
</entry>
</map>
</property>
<property name="schema" value="classpath:schemas/avs-pdf-query-request.xsd"/>
</bean>
<bean id="jaxbIntroductions" class="org.jboss.jaxb.intros.IntroductionsConfigParser"
factory-method="parseConfig">
<constructor-arg><value>classpath:spring/jaxb-intros-marshaller-mapping.xml</value></constructor-arg>
</bean>
现在在我的 webconfig.java 中我有
@Bean
public Jaxb2Marshaller jaxb2Marshaller() throws SAXException {
org.springframework.core.io.Resource schema = new ClassPathResource("schemas/person-schema.xsd");
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(new Class[]{Person.class});
marshaller.setSchema(schema);
return marshaller;
}
我知道我的 java 配置中缺少很多内容,我是 java 配置的新手,找不到很多有关如何执行此操作的文档。
任何帮助将不胜感激!
最佳答案
当 xml 配置到 java 配置中的给定项目缺乏文档时,您最好的 friend 是找到所涉及类的 javadoc,在这种情况下:
@Bean javadoc , Jaxb2Marshaler javadoc , JAXBRIContext javadoc ,并且找不到IntroductionsConfigParser和IntroductionsAnnotationReader的javadoc,也许需要主动订阅red hat?对此不太确定。
我自己从未使用过这些类,所以也许可能会出现错误,但我的第一次尝试如下所示:
@Bean(name = "jaxb2Marshaller")
public JaxbIntroductionsMarshaller jaxb2Marshaller() throws SAXException {
JaxbIntroductionsMarshaller marshaller = new JaxbIntroductionsMarshaller ();
marshaller.setClassesToBeBound(new Class[] {
Person.class,
WebServiceResponse.class
});
Map<String,Object> jaxbContextProperties = new HashMap<String,Object>();
jaxbContextProperties.put(JAXBRIContext.ANNOTATION_READER, introductionsAnnotationReader());
marshaller.setJaxbContextProperties(jaxbContextProperties);
Resource schema = new ClassPathResource("schemas/avs-pdf-query-request.xsd");
marshaller.setSchema(schema);
return marshaller;
}
@Bean
public IntroductionsAnnotationReader introductionsAnnotationReader() {
return new IntroductionsAnnotationReader(parseConfig());
}
现在不太确定工厂方法,所以我将发布我的 2 个可能的选项:
@Bean(name = "jaxbIntroductions") // bean id
public IntroductionsConfigParser parseConfig() { // factory-method
return new IntroductionsConfigParser( /* classpath:spring/jaxb-intros-marshaller-mapping.xml, Resource or String?? lack of javadoc */ );
}
或
@Bean(name = "jaxbIntroductions") // bean id
public IntroductionsConfigParser parseConfig() {
return IntroductionsConfigParser.parseConfig( /* Resource or String?? lack of javadoc */ );
}
关于java - Spring - 使用 java 配置为 xml 配置 Jboss Intros?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30715602/