Skip to content

Instantly share code, notes, and snippets.

@snicoll
Created February 26, 2014 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snicoll/9226274 to your computer and use it in GitHub Desktop.
Save snicoll/9226274 to your computer and use it in GitHub Desktop.
package org.springframework.web.servlet.mvc.method.annotation;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.SocketUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/**
*
* @author Stephane Nicoll
*/
public class PathVariableEncodingIntegrationTests {
private static Server server;
private static String baseUrl;
private RestTemplate restTemplate;
@BeforeClass
public static void startServer() throws Exception {
int port = SocketUtils.findAvailableTcpPort();
baseUrl = "http://localhost:" + port;
server = new Server(port);
ServletContextHandler handler = new ServletContextHandler();
handler.setContextPath("/");
ServletHolder pathVariableFullPathServlet = new ServletHolder(DispatcherServlet.class);
pathVariableFullPathServlet.setInitParameter(
"contextConfigLocation", PathVariableFullPathWebConfig.class.getName());
pathVariableFullPathServlet.setInitParameter(
"contextClass", AnnotationConfigWebApplicationContext.class.getName());
handler.addServlet(pathVariableFullPathServlet, "/path-variable-full-path/");
server.setHandler(handler);
server.start();
}
@Before
public void setUp() {
restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
}
@AfterClass
public static void stopServer() throws Exception {
if (server != null) {
server.stop();
}
}
@Test
public void decodePathVariableWithFullPath() {
String url = baseUrl + "/path-variable-full-path/tag/mmm%C3%A6%C3%A6%C3%B8%C3%A5";
String result = restTemplate.getForObject(url, String.class);
System.out.println(result);
}
@Configuration
static class PathVariableFullPathWebConfig extends WebMvcConfigurationSupport {
@Override
@Bean
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
RequestMappingHandlerMapping mapping = super.requestMappingHandlerMapping();
mapping.setAlwaysUseFullPath(true);
return mapping;
}
@Bean
public PathVariableEncodingController pathVariableEncodingController() {
return new PathVariableEncodingController();
}
}
@Controller
static class PathVariableEncodingController {
@RequestMapping(value = "/tag/{tagId}", method = RequestMethod.GET)
@ResponseBody
public String updateTag(@PathVariable("tagId") String tagId) {
return tagId;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment