「Spring MVC」コントローラでの戻り値(json,map,String)を実装する
1.json
@RequestMapping(“/getData")
@ResponseBody
public String getData(@RequestParam String name,@RequestParam String password) throws IOException{
System.out.println(name+" : “+password);
UserMember mm=new UserMember();
mm.setName(“yamada");
mm.setAge(“23");
mm.setAddress(“oosaki");
ObjectMapper objectMapper = new ObjectMapper();
String jsonStr=objectMapper.writeValueAsString(dog);
System.out.println(jsonStr);
return jsonStr;
}
2.void
@RequestMapping(“/index")
public void index() {
return;
}
3.String
@RequestMapping(value="/print")
@ResponseBody
public String print(){
String message = “テスト検証!";
return message;
}
4.Map
@RequestMapping(“/demo/show")
public Map<String, String> getMap() {
Map<String, String> map = new HashMap<String, String>();
map.put(“key1", “value1");
map.put(“key2", “value2");
return map;
}
5.ModelAndView
@RequestMapping(“/hello")
public ModelAndView helloWorld() {
String message = “テスト検証!";
return new ModelAndView(“hello", “message", message);
}