Skip to content

Instantly share code, notes, and snippets.

@emersonf
Created June 17, 2014 06:39
Show Gist options
  • Save emersonf/d656611d174e32a3d9d3 to your computer and use it in GitHub Desktop.
Save emersonf/d656611d174e32a3d9d3 to your computer and use it in GitHub Desktop.
A controller showing unexpected behaviour when URI template variables have regex restrictions.
package internal.sandbox.configuration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @author emerson
*/
@Configuration
@EnableAutoConfiguration
@ComponentScan("internal.sandbox")
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).run(args);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
package internal.sandbox.controller;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Map;
/**
* @author emerson
*/
@Controller
public class SomeController {
@RequestMapping(value = "/withoutRegex/{userId}")
public ResponseEntity<?> getUser(@PathVariable long userId,
@MatrixVariable(pathVar = "userId") Map<String, List<String>> matrixVariables) {
System.out.println(userId);
System.out.println(matrixVariables);
return new ResponseEntity<Object>(HttpStatus.OK);
}
@RequestMapping(value = "/withRegex/{userId:6}")
public ResponseEntity<?> getSpecificUser(@PathVariable long userId,
@MatrixVariable(pathVar = "userId") Map<String, List<String>> matrixVariables) {
System.out.println(userId);
System.out.println(matrixVariables);
return new ResponseEntity<Object>(HttpStatus.OK);
}
}
package internal.sandbox.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
/**
* @author emerson
*/
@Configuration
public class WebMvcConfiguration extends DelegatingWebMvcConfiguration {
@Override
@Bean
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
RequestMappingHandlerMapping mapping = super.requestMappingHandlerMapping();
mapping.setRemoveSemicolonContent(false);
return mapping;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment