Announcement Announcement Module
Collapse
No announcement yet.
Spring3 MVC annotation driven Rest Services in OSGi environment bean validation issue Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring3 MVC annotation driven Rest Services in OSGi environment bean validation issue

    I have developed Spring MVC annotation based OSGIfied Rest Web services and I deploy these services in Spring DM server (Virgo Server). I have issues with RequestBody validations. I am trying to use JSR 303 validation. JSR validations dont get triggered with Spring 3.05 and Spring 3.1.0.

    Spring 3.1.1 supports JSR 303 and the combination of @Valid and @RequestBody annotations triggers the validation.

    The latest Virgo server (3.6.1) has Spring 3.1.0. I had to upgrade the Virgo server to Spring 3.1.1 but still the validations are not being triggered.

    I doubt if JSR 303 validations really work with Spring DM. Has anyone ever used this combination? Can someone please suggest other ways to achieve this.



    Please Note: I have a POC for this combination (@Valid, @RequestBody, Spring 3.1.1)and I deploy that to a simple Jetty server and it works like charm. Same thing doesn't work in Virgo (Jetty+OSGi) Server.
    Last edited by puneetpandey; Mar 27th, 2013, 07:09 AM.

  • #2
    I have developed OSGi bundles of Rest web Services. I have used Spring Dynamic Module (Spring DM) for OSGi and Spring MVC (@Controller annotation) for Rest. I use JAXB to marshal and un-marshal @RequestBody and @ResponseBody respectively. I deploy my services in Spring DM server - Virgo Server.

    I need to validate Model object that is received as xml for POST and PUT requests. I have a POC that uses @Valid annotation with @RequestBody and @NotNull annotation in one of the variables in Model class. I use Spring 3.1.1 and it works like gem.

    The latest version of Virgo uses Spring 3.1.0, but, @Valid annotation only works with Spring 3.1.1.

    I have upgraded the Virgo server with Spring 3.1.1 but still validations are not triggered.




    Below is my Code:


    CONTROLLER:
    package com.stw.sams.attendance.overtime.api;

    import javax.validation.Valid;
    import org.springframework.http.HttpStatus;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.MethodArgumentNotVali dException;
    import org.springframework.web.bind.annotation.ExceptionH andler;
    import org.springframework.web.bind.annotation.RequestBod y;
    import org.springframework.web.bind.annotation.RequestMap ping;
    import org.springframework.web.bind.annotation.RequestMet hod;
    import org.springframework.web.bind.annotation.ResponseBo dy;
    import org.springframework.web.bind.annotation.ResponseSt atus;
    import com.stw.sams.model.Status;
    import com.stw.sams.model.UserVo;

    @Controller
    public class OverTimeController {

    @RequestMapping(value = "/overtime", method = RequestMethod.POST)
    @ResponseBody
    UserVo createOverTime(@Valid @RequestBody UserVo userVo) {

    UserVo userVo2 = new UserVo();
    userVo2.setEmail("sacd");
    userVo2.setId(1l);
    userVo2.setPassword("sc");
    System.out.println("done");

    return userVo2;

    }

    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MethodArgumentNotValidException. class)
    public @ResponseBody
    Status handleException(Exception exception) {

    Status status = new Status();
    status.setErrorMessage(exception.getMessage());

    return status;
    }

    }


    ----------------------------------------------------------------------------------------------------------------------

    xml Configuration:



    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:osgi="http://www.springframework.org/schema/osgi" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/osgi
    http://www.springframework.org/schem...g-osgi-1.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schem...ng-mvc-3.1.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schem...ring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schem...ng-context.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


    <bean
    class="org.springframework.web.servlet.mvc.annotat ion.DefaultAnnotationHandlerMapping" />

    <bean
    class="org.springframework.web.servlet.mvc.annotat ion.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
    <list>
    <ref bean="marshallingHttpMessageConverter" />
    </list>
    </property>

    </bean>

    <bean id="marshallingHttpMessageConverter"
    class="org.springframework.http.converter.xml.Mars hallingHttpMessageConverter"
    p:marshaller-ref="jaxb2Marshaller" p:unmarshaller-ref="jaxb2Marshaller" />

    <bean id="methodHandlerExceptionResolver" class="org.springframework.web.servlet.mvc.annotat ion.AnnotationMethodHandlerExceptionResolver" p:order="1">
    <property name="messageConverters">
    <list>
    <ref bean="marshallingHttpMessageConverter"/>
    </list>
    </property>
    </bean>

    <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshalle r">
    <property name="classesToBeBound">
    <list>
    <value>com.stw.sams.model.UserVo</value>
    <value>com.stw.sams.model.Status</value>
    </list>
    </property>
    </bean>

    <context:component-scan base-package="com.stw.sams.*" />

    <mvc:annotation-driven />


    </beans>



    ----------------------------------------------------------------------------------------------------------

    POM:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.samsungtechwin.sams</groupId>
    <artifactId>attendance-api</artifactId>
    <version>1.0.0</version>
    <packaging>bundle</packaging>
    <name>attendance-api</name>

    <properties>
    <org.springframework.version>3.1.0.RELEASE</org.springframework.version>
    </properties>

    <dependencies>


    <dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.0.0.GA</version>
    </dependency>

    <!-- <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>4.2.0.Final</version>
    </dependency> -->

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>${org.springframework.version}</version>
    <scope>test</scope>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-oxm</artifactId>
    <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${org.springframework.version}</version>
    </dependency>

    </dependencies>


    <build>
    <plugins>
    <plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <extensions>true</extensions>
    <version>2.2.0</version>
    <configuration>
    <manifestLocation>META-INF</manifestLocation>
    <instructions>
    <_wab>src/main/webapp/</_wab>
    <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
    <Bundle-Name>${project.name}</Bundle-Name>
    <Bundle-Version>${project.version}</Bundle-Version>
    <DynamicImport-Package>*</DynamicImport-Package>
    <Import-Bundle>
    org.eclipse.virgo.web.dm,
    org.springframework.beans;version="[3.0.5.RELEASE,3.1.0.RELEASE]",
    org.springframework.context;version="[3.0.5.RELEASE,3.1.0.RELEASE]",
    org.springframework.aop;version="[3.0.5.RELEASE,3.1.0.RELEASE]",
    com.springsource.org.aopalliance;version="[1.0.0,1.0.0]",
    org.springframework.jdbc;version="[3.0.5.RELEASE,3.1.0.RELEASE]",
    org.springframework.web.servlet;version="[3.0.5.RELEASE,3.1.0.RELEASE]"
    </Import-Bundle>
    <Include-Resource>src/main/webapp,src/main/resources</Include-Resource>
    <Web-ContextPath>attendance</Web-ContextPath>
    </instructions>
    </configuration>
    </plugin>
    </plugins>
    </build>




    </project>
    Last edited by puneetpandey; Mar 27th, 2013, 06:01 AM.

    Comment

    Working...
    X