Spring MVC 4.1 No. 009 HttpEntityで受け取る
今回はSpringで用意されているHttpEntityクラスでリクエストを受け付けます。
package com.example.spring.controller.c009; import org.springframework.http.HttpEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/c009") public class C009Controller { @RequestMapping("/entityForm") public String entityForm() { return "c009/entityForm"; } @RequestMapping(value = "/entityRecv", method = RequestMethod.POST) public String entityRecv(HttpEntity<String> httpEntity, Model model) { model.addAttribute("body", httpEntity.getBody()); return "c009/entityRecv"; } }
POSTデータ送信用のJSP、entityForm.jspです。
<%@page contentType="text/html; charset=utf-8" %><%-- --%><!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>サンプル</title> </head> <body> <form action="entityRecv" method="post"> 名前: <input type="text" name="name" size="20"><br> 年齢: <input type="text" name="age" size="20"><br> <input type="submit" value="送信"> </form> </body> </html>
リクエストボディ表示用のentityRecv.jspです。
<%@page contentType="text/html; charset=utf-8" %><%-- --%><!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>サンプル</title> </head> <body> nameの値は <c:out value="${name}" /><br> ageの値は <c:out value="${age}" /><br> bodyの値は <c:out value="${body}" /><br> </body> </html>
確認用のテストケースは次のとおりです。
package com.example.spring.controller.c009; import static org.hamcrest.CoreMatchers.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.web.context.WebApplicationContext; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring/spring-context.xml" }) public class C009ControllerTest { @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void setup() { mockMvc = webAppContextSetup(wac).build(); } @Test public void entityFormのGET() throws Exception { mockMvc.perform(get("/c009/entityForm")).andExpect(status().isOk()) .andExpect(view().name("c009/entityForm")); } @Test public void entityRecvのPOST() throws Exception { mockMvc.perform(post("/c009/entityRecv").content("name=test&age=33")) .andExpect(status().isOk()) .andExpect(view().name("c009/entityRecv")) .andExpect(model().attributeExists("body")) .andExpect(model().attribute("body", is("name=test&age=33"))) .andExpect(model().attributeDoesNotExist("name", "age")); } }
ソースは https://github.com/kuwalab/spring-mvc41 にあります。タグ009が今回のサンプルです。
まとめ http://kuwalab.hatenablog.jp/entry/spring
最初 http://kuwalab.hatenablog.jp/entry/spring_mvc41/001
前回 http://kuwalab.hatenablog.jp/entry/spring_mvc41/008
次回 http://kuwalab.hatenablog.jp/entry/spring_mvc41/010