请求映射路径
- 多人开发,每人设置不同的请求路径,冲突问题如何解决?
设置模块名作为请求路径前缀
| @Controller |
| |
| @RequestMapping("/hello") |
| public class HelloController { |
| |
| @RequestMapping("/save") |
| @ResponseBody |
| public String save(){ |
| return "{'module':'springmvc'}"; |
| } |
| |
| @RequestMapping("/delete") |
| @ResponseBody |
| public String delete(){ |
| return "{'module':'springmvc delete'}"; |
| } |
| } |
请求方式
get请求
| @Controller |
| public class HelloController { |
| |
| @RequestMapping("/save") |
| @ResponseBody |
| public String save(String name,int age){ |
| return "name = "+ name+"\n"+"age = "+ age; |
| } |
| } |
测试结果

post请求
java程序不变,post使用请求体发送表单

post请求中文乱码的情况(在Servlet容器内设置过滤器)
| public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer { |
| @Override |
| protected Class<?>[] getRootConfigClasses() { |
| return new Class[]{SpringConfig.class}; |
| } |
| |
| @Override |
| protected Class<?>[] getServletConfigClasses() { |
| return new Class[]{{SpringMVCConfig.class}}; |
| } |
| |
| @Override |
| protected String[] getServletMappings() { |
| return new String[]{"/"}; |
| } |
| |
| @Override |
| protected Filter[] getServletFilters() { |
| CharacterEncodingFilter filter = new CharacterEncodingFilter("UTF-8"); |
| return new Filter[]{filter}; |
| } |
| } |
| |
请求参数(一般类型)
1. 绑定请求参数和形参的映射关系
| @Controller |
| public class HelloController { |
| |
| @RequestMapping("/save") |
| @ResponseBody |
| |
| |
| public String save(@RequestParam("name")String username,int age){ |
| return "name = "+ name+"\n"+"age = "+ age; |
| } |
| } |
2. 实体类传参
| @Controller |
| public class HelloController { |
| |
| @RequestMapping("/user") |
| @ResponseBody |
| |
| public String save(User user){ |
| System.out.println(user); |
| return user.toString();; |
| } |
| } |
| public class User { |
| |
| |
| private String username; |
| private int age ; |
| private Address address; |
| |
| public String getUsername() { |
| return username; |
| } |
| |
| public void setUsername(String username) { |
| this.username = username; |
| } |
| |
| public int getAge() { |
| return age; |
| } |
| |
| public void setAge(int age) { |
| this.age = age; |
| } |
| |
| public Address getAddress() { |
| return address; |
| } |
| |
| public void setAddress(Address address) { |
| this.address = address; |
| } |
| |
| @Override |
| public String toString() { |
| return "User{" + |
| "username='" + username + '\'' + |
| ", age=" + age + |
| ", address=" + address + |
| '}'; |
| } |
| } |
| public class Address { |
| |
| |
| private String location; |
| |
| public String getLocation() { |
| return location; |
| } |
| |
| public void setLocation(String location) { |
| this.location = location; |
| } |
| |
| @Override |
| public String toString() { |
| return "Address{" + |
| "location='" + location + '\'' + |
| '}'; |
| } |
| } |
结果

3. 数组传参
| @Controller |
| public class HelloController { |
| @RequestMapping("/array") |
| @ResponseBody |
| public String user(String[] hobby){ |
| System.out.println(Arrays.toString(hobby)); |
| return Arrays.toString(hobby); |
| } |
| } |

4. 集合传参
| @Controller |
| public class HelloController { |
| @RequestMapping("/list") |
| @ResponseBody |
| public String user(@RequestParam List<String> list){ |
| System.out.println(list.toString()); |
| return list.toString(); |
| } |
| } |

json数据传输
- pom文件中导入依赖包
| <dependency> |
| <groupId>com.fasterxml.jackson.core</groupId> |
| <artifactId>jackson-databind</artifactId> |
| <version>2.9.0</version> |
| </dependency> |
| |
- 在配置类加上注解
| @Configuration |
| @ComponentScan({"controller"}) |
| |
| @EnableWebMvc |
| public class SpringMVCConfig{ |
| |
| } |
- 在控制器类里面对应的方法中添加注解
| @RequestMapping("/json/list") |
| @ResponseBody |
| |
| public String jsuser(@RequestBody List<String> list){ |
| System.out.println(list.toString()); |
| return list.toString(); |
| } |
| |
| @RequestMapping("/json/users") |
| @ResponseBody |
| public String jsuserlist(@RequestBody List<User> list){ |
| System.out.println(list.toString()); |
| return list.toString(); |
| } |
| |


@RequestBody与@RequestParam区别
区别:
- @RequestBody:用于接收url地址传参,表单传参
- @RequestParam:用于接收json数据
应用
- 后期开发中,发送json格式数据为主,@RequestParam使用较广
- 发送非json数据格式,使用@RequestBody
日期类型参数传参
| |
| @RequestMapping("/date") |
| @ResponseBody |
| public String date(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date date){ |
| System.out.println(date); |
| return date.toString(); |
| } |

实现原理
通过类型转换器Converter接口将一种类型转换为另一种类型
| public interface Converter<S,T> { |
| @Nullable |
| T convert(S var1); |
| } |
响应
在控制器类中定义以下功能函数
jsp页面
| @RequestMapping("/page") |
| public String toPage(){ |
| System.out.println("打开page页面"); |
| return "page.jsp"; |
| } |
文本
| @RequestMapping("/text") |
| @ResponseBody |
| public String toText(){ |
| System.out.println("打开text"); |
| return "text"; |
| } |
json对象
| @RequestMapping("/useroutput") |
| @ResponseBody |
| public User toUser(){ |
| User user = new User("LK", 12, null); |
| System.out.println("打开User"); |
| return user; |
| } |

响应@ResponseBody

HttpMessageConvertver
:专门的web转换接口

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构