rest - Spring MockMvc trimming parameter -


i have run problem has me stumped. running latest version of spring , junit. experimenting mockmvc , spring integration test classes in order see if fit companies needs , working well, until tried submit request contained @pathvariable version number in url.

pom;

<modelversion>4.0.0</modelversion>  <parent>         <groupid>org.springframework.boot</groupid>         <artifactid>spring-boot-starter-parent</artifactid>         <version>1.2.3.release</version>     </parent>   <groupid>asd</groupid>   <artifactid>asd</artifactid>   <version>0.0.1-snapshot</version>   <packaging>jar</packaging>    <name>asd</name>   <url>http://maven.apache.org</url>    <properties>     <project.build.sourceencoding>utf-8</project.build.sourceencoding>     <spring.version>4.1.6.release</spring.version>   </properties>    <dependencies>   <dependency>             <groupid>org.springframework.boot</groupid>             <artifactid>spring-boot-starter-web</artifactid>             <scope>compile</scope>         </dependency>     <dependency>             <groupid>junit</groupid>             <artifactid>junit</artifactid>             <version>4.12</version>             <scope>test</scope>         </dependency>         <dependency>             <groupid>junit</groupid>             <artifactid>junit-dep</artifactid>             <version>4.5</version>             <scope>test</scope>         </dependency>          <dependency>             <groupid>org.springframework</groupid>             <artifactid>spring-test</artifactid>             <version>${spring.version}</version>             <scope>test</scope>         </dependency>    </dependencies> 

example controller; import junit.framework.assert;

import org.springframework.web.bind.annotation.pathvariable; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.responsebody; import org.springframework.web.bind.annotation.restcontroller;  @restcontroller public class examplecontroller {      @requestmapping(value = "/example/appversion/{applicationversion}")     public @responsebody void example(@pathvariable("applicationversion") string applicationversion) {          if (!"1.0.0.0".equals(applicationversion)) {             assert.fail("i needed 1.0.0.0");         }      } } 

test class;

import static org.springframework.test.web.servlet.request.mockmvcrequestbuilders.get; import static org.springframework.test.web.servlet.result.mockmvcresulthandlers.print; import static org.springframework.test.web.servlet.result.mockmvcresultmatchers.status; import static org.springframework.test.web.servlet.setup.mockmvcbuilders.webappcontextsetup;  import org.junit.assert; import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.test.webintegrationtest; import org.springframework.test.context.contextconfiguration; import org.springframework.test.context.junit4.springjunit4classrunner; import org.springframework.test.web.servlet.mvcresult; import org.springframework.web.context.webapplicationcontext;  @runwith(springjunit4classrunner.class) @contextconfiguration(locations = "classpath*:appcontext.xml") @webintegrationtest public class exampleproblem {      @autowired     webapplicationcontext context;      @test     public void test() throws exception {         string url = "/example/appversion/1.0.0.0";         // string url = "/example/appversion/{applicationversion}";          //@formatter:off         mvcresult result =  webappcontextsetup(context)         .build()         .perform(             get(url) //                              get(url, "1.0.0.0")             .accept("application/json")         ).anddo(print())         .andexpect(status().is(200))         .andreturn();         //@formatter:on          assert.assertnotnull(result);     } } 

my appcontext.xml file contains line (other initial beans tag , end tag);

<context:component-scan base-package="asd" /> 

when view request coming example controller, can see debugger says /example/appversion/1.0.0.0 path variable being set "1.0.0" always. appears last .0 being dropped because if pass in "1.0.0.0.0" trims expected "1.0.0.0".

any insight may causing or doing wrong appreciated.

thank you.

this happens because spring interprets last dot (.) , following characters file extension.

in order fix this, need tell spring parameter expects dot characters adding ":.+" end of controller parameter. annotation looks this;

@requestmapping(value = "/example/appversion/{applicationversion:.+}") 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -