Skip to content

Commit 23077db

Browse files
committed
Add demo of matrix variables
1 parent 3411dc3 commit 23077db

File tree

5 files changed

+69
-16
lines changed

5 files changed

+69
-16
lines changed

src/main/java/org/springframework/samples/mvc/data/RequestDataController.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.springframework.http.HttpEntity;
44
import org.springframework.stereotype.Controller;
55
import org.springframework.web.bind.annotation.CookieValue;
6+
import org.springframework.web.bind.annotation.MatrixVariable;
67
import org.springframework.web.bind.annotation.PathVariable;
78
import org.springframework.web.bind.annotation.RequestBody;
89
import org.springframework.web.bind.annotation.RequestHeader;
@@ -30,6 +31,20 @@ public class RequestDataController {
3031
return "Obtained 'var' path variable value '" + var + "'";
3132
}
3233

34+
@RequestMapping(value="{path}/simple", method=RequestMethod.GET)
35+
public @ResponseBody String withMatrixVariable(@PathVariable String path, @MatrixVariable String foo) {
36+
return "Obtained matrix variable 'foo=" + foo + "' from path segment '" + path + "'";
37+
}
38+
39+
@RequestMapping(value="{path1}/{path2}", method=RequestMethod.GET)
40+
public @ResponseBody String withMatrixVariablesMultiple (
41+
@PathVariable String path1, @MatrixVariable(value="foo", pathVar="path1") String foo1,
42+
@PathVariable String path2, @MatrixVariable(value="foo", pathVar="path2") String foo2) {
43+
44+
return "Obtained matrix variable foo=" + foo1 + " from path segment '" + path1
45+
+ "' and variable 'foo=" + foo2 + " from path segment '" + path2 + "'";
46+
}
47+
3348
@RequestMapping(value="header", method=RequestMethod.GET)
3449
public @ResponseBody String withHeader(@RequestHeader String Accept) {
3550
return "Obtained 'Accept' header '" + Accept + "'";

src/main/java/org/springframework/samples/mvc/messageconverters/MessageConvertersController.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import javax.validation.Valid;
44

5+
import org.springframework.http.HttpHeaders;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.http.MediaType;
8+
import org.springframework.http.ResponseEntity;
59
import org.springframework.stereotype.Controller;
610
import org.springframework.util.LinkedMultiValueMap;
711
import org.springframework.util.MultiValueMap;
@@ -65,8 +69,11 @@ public class MessageConvertersController {
6569
}
6670

6771
@RequestMapping(value="/json", method=RequestMethod.GET)
68-
public @ResponseBody JavaBean writeJson() {
69-
return new JavaBean("bar", "apple");
72+
public ResponseEntity<JavaBean> writeJson() {
73+
HttpHeaders headers = new HttpHeaders();
74+
headers.setContentType(MediaType.TEXT_PLAIN);
75+
return new ResponseEntity<JavaBean>(new JavaBean("bar", "apple"), headers , HttpStatus.OK);
76+
// return new JavaBean("bar", "apple");
7077
}
7178

7279
// AtomFeedHttpMessageConverter (requires Rome on the classpath - useful for serving Atom feeds)

src/main/webapp/WEB-INF/views/home.jsp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
<div id="tabs">
1313
<ul>
1414
<li><a href="#simple">Simple</a></li>
15-
<li><a href="#mapping">Mapping Requests</a></li>
16-
<li><a href="#data">Obtaining Request Data</a></li>
17-
<li><a href="#responses">Writing Responses</a></li>
15+
<li><a href="#mapping">Request Mapping</a></li>
16+
<li><a href="#data">Request Data</a></li>
17+
<li><a href="#responses">Response Writing</a></li>
1818
<li><a href="#messageconverters">Message Converters</a></li>
19-
<li><a href="#views">Rendering Views</a></li>
19+
<li><a href="#views">View Rendering</a></li>
2020
<li><a href="#convert">Type Conversion</a></li>
2121
<li><a href="#validation">Validation</a></li>
2222
<li><a href="<c:url value="/form" />" title="forms">Forms</a></li>
2323
<li><a href="<c:url value="/fileupload" />" title="fileupload">File Upload</a></li>
2424
<li><a href="#exceptions">Exception Handling</a></li>
2525
<li><a href="#redirect">Redirecting</a></li>
26-
<li><a href="#async">Async</a></li>
26+
<li><a href="#async">Async Requests</a></li>
2727
</ul>
2828
<div id="simple">
2929
<h2>Simple</h2>
@@ -40,7 +40,7 @@
4040
</ul>
4141
</div>
4242
<div id="mapping">
43-
<h2>Mapping Requests</h2>
43+
<h2>Request Mapping</h2>
4444
<p>
4545
See the <code>org.springframework.samples.mvc.mapping</code> package for the @Controller code
4646
</p>
@@ -86,7 +86,7 @@
8686
</ul>
8787
</div>
8888
<div id="data">
89-
<h2>Obtaining Request Data</h2>
89+
<h2>Request Data</h2>
9090
<p>
9191
See the <code>org.springframework.samples.mvc.data</code> package for the @Controller code
9292
</p>
@@ -100,6 +100,12 @@
100100
<li>
101101
<a id="var" class="textLink" href="<c:url value="/data/path/foo" />">Path variable</a>
102102
</li>
103+
<li>
104+
<a id="matrixVar" class="textLink" href="<c:url value="/data/matrixvars;foo=bar/simple" />">Matrix variable</a>
105+
</li>
106+
<li>
107+
<a id="matrixVarMultiple" class="textLink" href="<c:url value="/data/matrixvars;foo=bar1/multiple;foo=bar2" />">Matrix variables (multiple)</a>
108+
</li>
103109
<li>
104110
<a id="header" class="textLink" href="<c:url value="/data/header" />">Header</a>
105111
</li>
@@ -154,7 +160,7 @@
154160
</div>
155161
</div>
156162
<div id="responses">
157-
<h2>Writing Responses</h2>
163+
<h2>Response Writing</h2>
158164
<p>
159165
See the <code>org.springframework.samples.mvc.response</code> package for the @Controller code
160166
</p>
@@ -262,7 +268,7 @@
262268
</div>
263269
</div>
264270
<div id="views">
265-
<h2>Rendering Views</h2>
271+
<h2>View Rendering</h2>
266272
<p>
267273
See the <code>org.springframework.samples.mvc.views</code> package for the @Controller code
268274
</p>
@@ -385,7 +391,7 @@
385391
</ul>
386392
</div>
387393
<div id="async">
388-
<h2>Async</h2>
394+
<h2>Async Requests</h2>
389395
<p>
390396
<em>Note: Links may take 2-3 seconds to complete.</em>
391397
</p>

src/test/java/org/springframework/samples/mvc/data/DataControllerTests.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,24 @@
55
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
66
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
77
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
8-
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
8+
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
99

1010
import org.junit.Before;
1111
import org.junit.Test;
12+
import org.junit.runner.RunWith;
1213
import org.springframework.http.MediaType;
14+
import org.springframework.samples.mvc.AbstractContextControllerTests;
15+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
1316
import org.springframework.test.web.servlet.MockMvc;
1417

15-
public class DataControllerTests {
18+
@RunWith(SpringJUnit4ClassRunner.class)
19+
public class DataControllerTests extends AbstractContextControllerTests {
1620

1721
private MockMvc mockMvc;
1822

1923
@Before
2024
public void setup() throws Exception {
21-
this.mockMvc = standaloneSetup(new RequestDataController()).alwaysExpect(status().isOk()).build();
25+
this.mockMvc = webAppContextSetup(this.wac).alwaysExpect(status().isOk()).build();
2226
}
2327

2428
@Test
@@ -35,11 +39,23 @@ public void group() throws Exception {
3539
}
3640

3741
@Test
38-
public void pathvar() throws Exception {
42+
public void pathVar() throws Exception {
3943
this.mockMvc.perform(get("/data/path/foo"))
4044
.andExpect(content().string("Obtained 'var' path variable value 'foo'"));
4145
}
4246

47+
@Test
48+
public void matrixVar() throws Exception {
49+
this.mockMvc.perform(get("/data/matrixvars;foo=bar/simple"))
50+
.andExpect(content().string("Obtained matrix variable 'foo=bar' from path segment 'matrixvars'"));
51+
}
52+
53+
@Test
54+
public void matrixVarMultiple() throws Exception {
55+
this.mockMvc.perform(get("/data/matrixvars;foo=bar1/multiple;foo=bar2"))
56+
.andExpect(content().string("Obtained matrix variable foo=bar1 from path segment 'matrixvars' and variable 'foo=bar2 from path segment 'multiple'"));
57+
}
58+
4359
@Test
4460
public void header() throws Exception {
4561
this.mockMvc.perform(get("/data/header").accept(MediaType.ALL))

src/test/java/org/springframework/samples/mvc/messageconverters/MessageConvertersControllerTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
44
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
5+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
56
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
67
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
78
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -91,6 +92,14 @@ public void writeJson() throws Exception {
9192
.andExpect(jsonPath("$.fruit").value("apple"));
9293
}
9394

95+
@Test
96+
public void writeJson2() throws Exception {
97+
this.mockMvc.perform(get(URI, "json").accept(MediaType.APPLICATION_JSON))
98+
.andDo(print())
99+
.andExpect(jsonPath("$.foo").value("bar"))
100+
.andExpect(jsonPath("$.fruit").value("apple"));
101+
}
102+
94103
private static String ATOM_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
95104
"<feed xmlns=\"http://www.w3.org/2005/Atom\"><title>My Atom feed</title></feed>";
96105

0 commit comments

Comments
 (0)