Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use SOAP Webservices and Spring MVC together

I have a Spring MVC project. I wrote a code something like

@Controller
@RequestMapping("CallBack")
@WebService(name = "NotificationToCP", targetNamespace = "http://SubscriptionEngine.ibm.com")
public class CallbackController {

    @RequestMapping("")
    @ResponseBody
    @WebMethod(action = "notificationToCP")
    @RequestWrapper(localName = "notificationToCP", targetNamespace = "http://SubscriptionEngine.ibm.com", className = "in.co.mobiz.airtelVAS.model.NotificationToCP_Type")
    @ResponseWrapper(localName = "notificationToCPResponse", targetNamespace = "http://SubscriptionEngine.ibm.com", className = "in.co.mobiz.airtelVAS.model.NotificationToCPResponse")
    public NotificationToCPResponse index(
            @WebParam(name = "notificationRespDTO", targetNamespace = "") CPNotificationRespDTO notificationRespDTO) {
        return new NotificationToCPResponse();
    }
}

Can I use Spring MVC + Webservices together? What I want is just as a controller that accepts a SOAP request and process it. The url needs to be /CallBack. I'm still as a sort of confused being a Newbie. Will something like above work. Else how do I get it going.

like image 741
Akhil K Nambiar Avatar asked Oct 16 '25 01:10

Akhil K Nambiar


1 Answers

I wouldn't mix Spring MVC and SOAP webservice (JAX-WS) together since they serve different purpose.

Better practice is to encapsulate your business operation in a service class, and expose it using both MVC controller and JAX-WS. For example:

HelloService

@Service
public class HelloService {
    public String sayHello() {
        return "hello world";
    }
}

HelloController has HelloService reference injected via autowiring. This is standard Spring MVC controller that invoke the service and pass the result as a model to a view (eg: hello.jsp view)

@Controller
@RequestMapping("/hello")
public class HelloController {
    @Autowired private HelloService helloService;

    @RequestMapping(method = RequestMethod.GET)
    public String get(Model model) {
        model.addAttribute("message", helloService.sayHello());
        return "hello";
    }
}

A JAX-WS endpoint also invoke the same service. The difference is the service is exposed as a SOAP web service

@WebService(serviceName="HelloService")
public class HelloServiceEndpoint {
    @Autowired private HelloService helloService;

    @WebMethod
    public String sayHello() {
        return helloService.sayHello();
    }
}

Note that JAX-WS style web service above isn't guaranteed to automatically work on all Spring deployment, especially if deployed on non Java EE environment (tomcat). Additional setup might be required.

like image 144
gerrytan Avatar answered Oct 18 '25 17:10

gerrytan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!