java - Spring - 使用 java 配置为 xml 配置 Jboss Intros?

标签 java xml spring spring-mvc jboss

我正在编写一个 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/

相关文章:

java - 我正在尝试执行 jdbc 事务。但是一旦我运行代码,数据库就不会更新

objective-c - 将 Plist (NSString) 解析为 NSDictionary

java - HTTP 状态 500 - 数据库访问异常 : Connection Pool - Servlet Tomcat - MySQL

java - Spring oauth2 不重定向到原始 url

java - Spring - 将字符串或 JSONObject 返回到 JSONP

java - 无法从 eclipse/Intellij 发送 API 请求

Java swing)我想设计基于弹出窗口的界面

java - 为 HTTP 路径映射的处理程序方法不明确 同一 URI GET 和 POST 发生错误

java - Eclipse WST 打开新的 XML 向导抛出 java.lang.NullPointerException

java - 我可以轻松地用 spring-boot 替换 spring-boot-starter 以避免使用 spring-boot-starter-parent 吗?