学习了几种方式。采用@RequestBody注解的方式最简单。
一、通过maven的pom.xml文件导入jackson的包。
<dependency> <groupId>com.fasterxml.jackson.coregroupId> <artifactId>jackson-databindartifactId> <version>2.7.4version> dependency> <dependency> <groupId>com.fasterxml.jackson.coregroupId> <artifactId>jackson-coreartifactId> <version>2.7.4version> dependency> <dependency> <groupId>com.fasterxml.jackson.coregroupId> <artifactId>jackson-annotationsartifactId> <version>2.7.4version> dependency>二、配置servlet.xml文件
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" /> list> property> bean> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" /> list> property> bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name = "supportedMediaTypes"> <list> <bean class="org.springframework.http.MediaType"> <constructor-arg index="0" value="text"/> <constructor-arg index="1" value="plain"/> <constructor-arg index="2" value="UTF-8"/> bean> <bean class="org.springframework.http.MediaType"> <constructor-arg index="0" value="*"/> <constructor-arg index="1" value="*"/> <constructor-arg index="2" value="UTF-8"/> bean> <bean class="org.springframework.http.MediaType"> <constructor-arg index="0" value="text"/> <constructor-arg index="1" value="*"/> <constructor-arg index="2" value="UTF-8"/> bean> <bean class="org.springframework.http.MediaType"> <constructor-arg index="0" value="application"/> <constructor-arg index="1" value="json"/> <constructor-arg index="2" value="UTF-8"/> bean> list> property> bean>三、写接口,采用注解的方式
1.请求地址:http://192.168.2.23:8080/addstudent?username=liudehua&age=60
参数username和age要对应。
@RequestMapping(value = "addstudent",produces = "application/json;charset=UTF-8") @ResponseBody public Object addstudent(String username,int age){ // 返回数组 ListstudentList = new ArrayList (); Student stu = new Student(); stu.setName(username); stu.setAge(age); studentList.add(stu); stu = new Student(); stu.setName("wushan"); stu.setAge(20); studentList.add(stu); Map > map = new HashMap >(); map.put("studentList",studentList); return map; }
2.请求地址:http://192.168.2.23:8080/addstudent?username=liudehua&age=60
//通过HttpServletRequest接收,post方式和get方式都可以。 @RequestMapping(value = "addstudent",produces = "application/json;charset=UTF-8") @ResponseBody public Object addstudent(HttpServletRequest request) { String username = request.getParameter("username"); int age = Integer.valueOf(request.getParameter("age")); Student student = new Student(); student.setName(username); student.setAge(age); return student; }
3.请求地址:http://192.168.2.23:8080/addstudent?username=liudehua&age=60
username和age必须和Student的类的成员变量名称完全一致
//通过一个bean来接收,post方式和get方式都可以 @RequestMapping("addstudent") @ResponseBody public Object addstudent(Student student) { Mapmap = new HashMap (); map.put("student",student); return map; }
4.
请求地址:http://192.168.2.23:8080/addstudent/liudehua/60
//通过@PathVariable获取路径中的参数 @RequestMapping("addstudent/{username}/{age}") @ResponseBody public Object addstudent(@PathVariable String username,@PathVariable int age) { Student student = new Student(); student.setName(username); student.setAge(age); return student; }5.
//用注解@RequestParam绑定请求参数到方法入参 @RequestMapping("addstudent") @ResponseBody public Object addstudent(@RequestParam("username") String username,@RequestParam("age") int age) { Student student = new Student(); student.setName(username); student.setAge(age); return student; }