1

I'm trying to learn the basics of Spring 4 MVC for Restful web services, but that's not the problem here.

In my @RestController, I'd like to use Spring's Unmarshaller Interface, and specifically use Jaxb2Marshaller. So, for now, I have....

@RestController
@RequestMapping("/postuser")
public class MyController {
    private String xsdFileName = "User.xsd";
    private Jaxb2Marshaller unmarshaller;

    public MyController() {
        unmarshaller = new Jaxb2Marshaller();
        unmarshaller.setPackagesToScan("com.mypackage");
    }
// rest of class
}

which works. But how do I do the same thing either via @Autowired to set the unmarshaller, or use Spring's dependency injection via the XML configuration files?

My dispatcher-servlet.xml file is simple...

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:p="http://www.springframework.org/schema/p"
      xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

   <context:component-scan base-package="com.mypackage" />

   <mvc:annotation-driven />

 </beans>

Many Thanks for your help & suggestions.

Chris

Chris
  • 371
  • 2
  • 4
  • 15
  • Do you really need the marshaller autowired into the controller? The usual use of those is like http://stackoverflow.com/questions/26070566/returning-jaxb-generated-elements-from-spring-boot-controller in the "not working" section (which I guess works now). Register them with MVC, then let spring use them automatically to marshal all XML requests. – zapl Nov 17 '15 at 21:30
  • zapl: no, I don't have to have the marshaller autowired. That was only one way I thought of that might configure things. Spring has several ways to configure things -- I wasn't sure how to inject the Marshaller into the Controller correctly, instead of just hard coding it. – Chris Nov 17 '15 at 21:44

1 Answers1

1

You can use a configuration class

@Configuration
public MyClass{

    @Bean
    public Jaxb2Marshaller unmarshaller() {
        Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller();
        unmarshaller.setPackagesToScan("com.mypackage");
        return unmarshaller;
    }
}

then in your controller

@RestController
@RequestMapping("/postuser")
public class MyController {
    private String xsdFileName = "User.xsd";
    @Autowired
    private Jaxb2Marshaller unmarshaller;

// rest of class
}

The bean definition you can also make it in a xml file

<bean id="unmarshaller" class="package.Jaxb2Marshaller">
    <property name="packagesToScan">
        <value>com.mypackage</value>
    </property>
</bean>
reos
  • 7,402
  • 3
  • 23
  • 33