2023-09-08
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.2</version> </dependency>
ServletConfig
package com.hh.config; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; import javax.servlet.Filter; /** * @author hh * @version 1.0 * @DATE 2023-09-08 10:09:20 */ public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[0]; } @Override protected Class<?>[] getServletConfigClasses() { return new Class[]{ SpringmvcConfig.class }; } @Override protected String[] getServletMappings() { return new String[]{"/"}; } //设置中文乱码的问题 @Override protected Filter[] getServletFilters() { CharacterEncodingFilter filter = new CharacterEncodingFilter(); filter.setEncoding("utf-8"); return new Filter[]{filter}; } }
SpringmvcConfig
package com.hh.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; /** * @author hh * @version 1.0 * @DATE 2023-09-08 10:08:31 */ @Configuration @ComponentScan({"com.hh.controller"}) @EnableWebMvc//开启json转字符串 public class SpringmvcConfig { }
AccountController
package com.hh.controller; import com.hh.pojo.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import java.util.Arrays; import java.util.List; /** * @author hh * @version 1.0 * @DATE 2023-09-08 10:28:46 */ @Controller @RequestMapping("/account") public class AccountController { @RequestMapping("/listjson") @ResponseBody public String commonParamListJson(@RequestBody List<String> list){ System.out.println("数组参数arrs==>"+ list); System.out.println("AccountController commonParamList..."); return "{'info':'huahua is the best'}"; } }
postman中传递测试的值
测试传递pojo对象
@RequestMapping("/pojoJson") @ResponseBody public String pojoJson(@RequestBody User user){ System.out.println("普通参数pojo==>"+user); System.out.println("AccountController commonParamPojo..."); return "{'info':'huahua is the best'}"; }
测试集合
@RequestMapping("/listjson2") @ResponseBody public String listjson2(@RequestBody List<User> list){ System.out.println("集合参数list==>"+ list); System.out.println("AccountController listjson2..."); return "{'info':'huahua is the best'}"; }
标签:web,return,springmvc,jar,hh,springframework,bean,org,import From: https://www.cnblogs.com/isDaHua/p/17687155.html