Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Spring Validator on Web services

Is it possible to use Spring Validators to validate data from Web Services Soap requests? Or more so what should I change about the below approach to make it possible?

The precise context that I have is below:

I have a web front end using Freemarker and Controllers that works fine with validation for example using

<bean id="stockValidator" class="com.client.validator.StockValidator" />

In the dispatcher-servlet.xml

Then in the StockController the validation occurs on Post requests.

 @RequestMapping(value = "/addStock", method = RequestMethod.POST)
   public String addStudent(@ModelAttribute Stock stock,BindingResult result,
   ModelMap model ) {

       StockValidator.validate(stock, result );
       if (result.hasErrors()) {
            //model.addAttribute("stock", stock);
            return "stock";
        } else {
            StockService.save(stock);
            model.addAttribute("stockId", stock.getStockId());
            model.addAttribute("stockCode", stock.getStockCode());
            model.addAttribute("stockName", stock.getStockName());

           return "result";
        }
   }

However my SOAP web services are Annotation based wired into the services

import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;

import com.olympus.viewtheworld.server.dao.StockDao;
import com.olympus.viewtheworld.server.service.StockService;
import com.olympus.viewtheworld.shared.domain.Stock;

@WebService(endpointInterface = "com.server.service.StockService")
public class StockServiceImpl implements StockService{

@Autowired
StockDao stockDao;

This is mapped in the dispatcher servlet as such:

    <jaxws:endpoint id="stockService"
        implementorClass="com.server.service.Impl.StockServiceImpl"
        implementor="#stockServiceImpl"
        address="/SoapService/stock">
    <jaxws:serviceFactory>
        <ref bean="jaxws-and-aegis-service-factory"/>
    </jaxws:serviceFactory>
</jaxws:endpoint>

Sorry I am a hobby developer and think that somewhere along the way I have got a bit confused in how best to approach this setup. If it is more appropriate to start again from scratch let me know.

Cheers, Rob

like image 505
Rob Avatar asked Sep 04 '13 18:09

Rob


2 Answers

You might want to look into Spring Web Services (http://projects.spring.io/spring-ws/). With that you can use PayloadValidatingInterceptor to run XSD-based validation on incoming SOAP messages.

like image 174
Jukka Avatar answered Oct 20 '22 19:10

Jukka


I'm just adding example of how to use @Jukka's suggested PayloadValidatingInterceptor.

You can PayloadValidatingInterceptor to intercept requests and validate it with XSD schema. E.g. (applicationContext.xml):

<sws:interceptors>
        <!-- Add our validating interceptor -->
        <ref bean="validatingInterceptor" />
    </sws:interceptors>
<bean id="validatingInterceptor" class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor">
        <property name="schema" value="classpath:org/example/myproject/xsd/myprojectws.xsd"/>
        <property name="validateRequest" value="true"/>
        <property name="validateResponse" value="true"/>
    </bean>
like image 21
Ernestas Kardzys Avatar answered Oct 20 '22 18:10

Ernestas Kardzys