在JAXB XmlAdapters中使用注入Spring RESTful Controller [英] Using injection in JAXB XmlAdapters for Spring RESTful Controller

查看:54
本文介绍了在JAXB XmlAdapters中使用注入Spring RESTful Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序需要 JAXB XmlAdapter 的实例,例如

My application requires instances of the JAXB XmlAdapter such as

public class CategoryTypeAdapter extends XmlAdapter<String, Category> {

    @Autowired
    protected CategoryService categoryService;

    public CategoryTypeAdapter() {
    }

    @Override
    public String marshal(Category v) throws Exception {
        return (v == null) ? null : v.getId();
    }

    @Override
    public Category unmarshal(String v) throws Exception {
        // noop
        if (StringUtils.isBlank(v)) {
            return null;
        }
        // load via service
        return this.categoryService.getCategory(v);
    }
}

问题:如何配置Spring的 Jaxb2RootElementHttpMessageConverter 在适当的mashaller中设置这个适配器?

Question: How can I configure Spring's Jaxb2RootElementHttpMessageConverter to set this adapter in the appropriate mashaller?

我知道有一个Spring OXM库,但我不知道我是怎么做的可以将OXM类与我的注释驱动的REST控制器一起使用。

I know there is the Spring OXM library but I am not clear on how I can use the OXM classes together with my annotation driven REST controllers.

推荐答案

您不能将XmlAdapter与Spring的消息转换器关联,而是使用正常的JAXB机制,用你需要它的类型注册这个XmlAdapter:

You cannot associate an XmlAdapter to Spring's message converters, instead use the normal JAXB mechanism to register this XmlAdapter with the type where you require it:

public class MyRootElement{

   @XmlJavaTypeAdapter(CategoryTypeAdapter.class)
   private Category category;

}

现在关于如何注入<$ c的问题$ c> CategoryService 进入 CategoryTypeAdapter ,有几种方法可以将Spring依赖注入非Spring托管对象,可能是最简单的方法你有AspectJ基础设施将在你的适配器上使用 @Configurable 注释。

Now the question of how to inject in a CategoryService into CategoryTypeAdapter, there are a couple of ways to inject in a Spring dependency into a non Spring managed object, probably the simplest if you have AspectJ infrastructure will be to use @Configurable annotation on your Adapter.

这篇关于在JAXB XmlAdapters中使用注入Spring RESTful Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆