Permalink
Browse files

Add demo of matrix variables

  • Loading branch information...
1 parent 3411dc3 commit 23077db9a4b6c0865536ea24d94f250d81dd9d0d @rstoyanchev rstoyanchev committed Oct 30, 2012
@@ -3,6 +3,7 @@
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
+import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
@@ -30,6 +31,20 @@
return "Obtained 'var' path variable value '" + var + "'";
}
+ @RequestMapping(value="{path}/simple", method=RequestMethod.GET)
+ public @ResponseBody String withMatrixVariable(@PathVariable String path, @MatrixVariable String foo) {
+ return "Obtained matrix variable 'foo=" + foo + "' from path segment '" + path + "'";
+ }
+
+ @RequestMapping(value="{path1}/{path2}", method=RequestMethod.GET)
+ public @ResponseBody String withMatrixVariablesMultiple (
+ @PathVariable String path1, @MatrixVariable(value="foo", pathVar="path1") String foo1,
+ @PathVariable String path2, @MatrixVariable(value="foo", pathVar="path2") String foo2) {
+
+ return "Obtained matrix variable foo=" + foo1 + " from path segment '" + path1
+ + "' and variable 'foo=" + foo2 + " from path segment '" + path2 + "'";
+ }
+
@RequestMapping(value="header", method=RequestMethod.GET)
public @ResponseBody String withHeader(@RequestHeader String Accept) {
return "Obtained 'Accept' header '" + Accept + "'";
@@ -2,6 +2,10 @@
import javax.validation.Valid;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
@@ -65,8 +69,11 @@
}
@RequestMapping(value="/json", method=RequestMethod.GET)
- public @ResponseBody JavaBean writeJson() {
- return new JavaBean("bar", "apple");
+ public ResponseEntity<JavaBean> writeJson() {
+ HttpHeaders headers = new HttpHeaders();
+ headers.setContentType(MediaType.TEXT_PLAIN);
+ return new ResponseEntity<JavaBean>(new JavaBean("bar", "apple"), headers , HttpStatus.OK);
+// return new JavaBean("bar", "apple");
}
// AtomFeedHttpMessageConverter (requires Rome on the classpath - useful for serving Atom feeds)
@@ -12,18 +12,18 @@
<div id="tabs">
<ul>
<li><a href="#simple">Simple</a></li>
- <li><a href="#mapping">Mapping Requests</a></li>
- <li><a href="#data">Obtaining Request Data</a></li>
- <li><a href="#responses">Writing Responses</a></li>
+ <li><a href="#mapping">Request Mapping</a></li>
+ <li><a href="#data">Request Data</a></li>
+ <li><a href="#responses">Response Writing</a></li>
<li><a href="#messageconverters">Message Converters</a></li>
- <li><a href="#views">Rendering Views</a></li>
+ <li><a href="#views">View Rendering</a></li>
<li><a href="#convert">Type Conversion</a></li>
<li><a href="#validation">Validation</a></li>
<li><a href="<c:url value="/form" />" title="forms">Forms</a></li>
<li><a href="<c:url value="/fileupload" />" title="fileupload">File Upload</a></li>
<li><a href="#exceptions">Exception Handling</a></li>
<li><a href="#redirect">Redirecting</a></li>
- <li><a href="#async">Async</a></li>
+ <li><a href="#async">Async Requests</a></li>
</ul>
<div id="simple">
<h2>Simple</h2>
@@ -40,7 +40,7 @@
</ul>
</div>
<div id="mapping">
- <h2>Mapping Requests</h2>
+ <h2>Request Mapping</h2>
<p>
See the <code>org.springframework.samples.mvc.mapping</code> package for the @Controller code
</p>
@@ -86,7 +86,7 @@
</ul>
</div>
<div id="data">
- <h2>Obtaining Request Data</h2>
+ <h2>Request Data</h2>
<p>
See the <code>org.springframework.samples.mvc.data</code> package for the @Controller code
</p>
@@ -101,6 +101,12 @@
<a id="var" class="textLink" href="<c:url value="/data/path/foo" />">Path variable</a>
</li>
<li>
+ <a id="matrixVar" class="textLink" href="<c:url value="/data/matrixvars;foo=bar/simple" />">Matrix variable</a>
+ </li>
+ <li>
+ <a id="matrixVarMultiple" class="textLink" href="<c:url value="/data/matrixvars;foo=bar1/multiple;foo=bar2" />">Matrix variables (multiple)</a>
+ </li>
+ <li>
<a id="header" class="textLink" href="<c:url value="/data/header" />">Header</a>
</li>
<li>
@@ -154,7 +160,7 @@
</div>
</div>
<div id="responses">
- <h2>Writing Responses</h2>
+ <h2>Response Writing</h2>
<p>
See the <code>org.springframework.samples.mvc.response</code> package for the @Controller code
</p>
@@ -262,7 +268,7 @@
</div>
</div>
<div id="views">
- <h2>Rendering Views</h2>
+ <h2>View Rendering</h2>
<p>
See the <code>org.springframework.samples.mvc.views</code> package for the @Controller code
</p>
@@ -385,7 +391,7 @@
</ul>
</div>
<div id="async">
- <h2>Async</h2>
+ <h2>Async Requests</h2>
<p>
<em>Note: Links may take 2-3 seconds to complete.</em>
</p>
@@ -5,20 +5,24 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
-import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
+import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.springframework.http.MediaType;
+import org.springframework.samples.mvc.AbstractContextControllerTests;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
-public class DataControllerTests {
+@RunWith(SpringJUnit4ClassRunner.class)
+public class DataControllerTests extends AbstractContextControllerTests {
private MockMvc mockMvc;
@Before
public void setup() throws Exception {
- this.mockMvc = standaloneSetup(new RequestDataController()).alwaysExpect(status().isOk()).build();
+ this.mockMvc = webAppContextSetup(this.wac).alwaysExpect(status().isOk()).build();
}
@Test
@@ -35,12 +39,24 @@ public void group() throws Exception {
}
@Test
- public void pathvar() throws Exception {
+ public void pathVar() throws Exception {
this.mockMvc.perform(get("/data/path/foo"))
.andExpect(content().string("Obtained 'var' path variable value 'foo'"));
}
@Test
+ public void matrixVar() throws Exception {
+ this.mockMvc.perform(get("/data/matrixvars;foo=bar/simple"))
+ .andExpect(content().string("Obtained matrix variable 'foo=bar' from path segment 'matrixvars'"));
+ }
+
+ @Test
+ public void matrixVarMultiple() throws Exception {
+ this.mockMvc.perform(get("/data/matrixvars;foo=bar1/multiple;foo=bar2"))
+ .andExpect(content().string("Obtained matrix variable foo=bar1 from path segment 'matrixvars' and variable 'foo=bar2 from path segment 'multiple'"));
+ }
+
+ @Test
public void header() throws Exception {
this.mockMvc.perform(get("/data/header").accept(MediaType.ALL))
.andExpect(content().string("Obtained 'Accept' header '*/*'"));
@@ -2,6 +2,7 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -91,6 +92,14 @@ public void writeJson() throws Exception {
.andExpect(jsonPath("$.fruit").value("apple"));
}
+ @Test
+ public void writeJson2() throws Exception {
+ this.mockMvc.perform(get(URI, "json").accept(MediaType.APPLICATION_JSON))
+ .andDo(print())
+ .andExpect(jsonPath("$.foo").value("bar"))
+ .andExpect(jsonPath("$.fruit").value("apple"));
+ }
+
private static String ATOM_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<feed xmlns=\"http://www.w3.org/2005/Atom\"><title>My Atom feed</title></feed>";

0 comments on commit 23077db

Please sign in to comment.