SpringMVC框架

SpringMVC框架 通过注解进行配置ServletContainersinitConfig.java方法一package com.movie.act.common; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer; //定义一个servlet容器启动的配置类 public class ServletContainersinitConfig extends AbstractDispatcherServletInitializer { //加载springmvc配置 Override protected WebApplicationContext createServletApplicationContext() { AnnotationConfigWebApplicationContext context new AnnotationConfigWebApplicationContext(); context.register(SpringMvcConfig.class); return context; } //设置都有哪些请求归属springmvc处理 Override protected String[] getServletMappings() { return new String[]{/}; } //加载spring配置容器 Override protected WebApplicationContext createRootApplicationContext() { return null; } }方法二package com.movie.act.common; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer; //定义一个servlet容器启动的配置类 public class ServletContainersinitConfig extends AbstractAnnotationConfigDispatcherServletInitializer { //加载spring配置容器 Override protected Class?[] getRootConfigClasses() { return new Class[0]; } //加载springmvc配置容器 Override protected Class?[] getServletConfigClasses() { return new Class[]{SpringConfig.class}; } //设置都有哪些请求归属springmvc处理 Override protected String[] getServletMappings() { return new String[]{/}; } }SpringMvcConfigpackage com.movie.act.common; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; Configuration ComponentScan(com.movie.act.controller) public class SpringMvcConfig { }usercontrollerpackage com.movie.act.controller; 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.ResponseBody; Controller public class usercontroller { RequestMapping(/) ResponseBody public String user(){ System.out.println(hello world); return user; } }使用主机配置的tomcat作为web服务器启动tomcat需要配置工件工件Artifact其实就是你的项目经过编译、打包后能够直接部署到 Tomcat 中运行的“产品”。可以把它理解成“待部署的软件包”。工件的两种常见形式WAR 包一个压缩文件例如myapp.war里面包含了编译后的类、依赖的 JAR 包、配置文件、JSP、静态资源等。把它放到 Tomcat 的webapps目录下Tomcat 启动时会自动解压并运行。Exploded 目录展开的目录一个文件夹里面的内容结构和一个解压后的 WAR 包完全一样。IDEA 通常使用这种形式因为它支持热部署你修改代码后IDEA 可以快速把更新后的文件复制到该目录下无需重启 Tomcat 就能看到效果方便开发调试。先设置工件MVC项目传递json数据需要配置依赖和EnableWebMvc注解dependency groupIdcom.fasterxml.jackson.core/groupId artifactIdjackson-databind/artifactId version2.15.2/version /dependency !-- 可选但明确指定更安全 -- dependency groupIdcom.fasterxml.jackson.core/groupId artifactIdjackson-core/artifactId version2.15.2/version /dependency dependency groupIdcom.fasterxml.jackson.core/groupId artifactIdjackson-annotations/artifactId version2.15.2/version /dependency启动tomcat后查看out路径是否加载依赖如果没有说明没有导入成功在SpringMvcConfig配置文件中加入EnableWebMvc注解在传统的 Spring MVC 项目如使用 XML 配置或纯 Java 配置中必须使用EnableWebMvc来启用注解驱动的 MVC 功能。通常搭配Configuration一起使用并可以实现WebMvcConfigurer接口来自定义配置如视图解析器、静态资源处理、消息转换器等。在SpringMvcConfig配置文件中添加配置Override public void configureMessageConverters(ListHttpMessageConverter? converters) { // 添加 Jackson JSON 转换器 使 Spring MVC 能够处理 JSON 格式的请求和响应 //如果没有此转换器控制器方法返回的对象如 ResponseBody ListUser无法被自动序列化为 JSON或接收 JSON 请求体时会报 415 错误。 MappingJackson2HttpMessageConverter jsonConverter new MappingJackson2HttpMessageConverter(); jsonConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON_UTF8)); converters.add(jsonConverter); }tomcat启动控制台打印中文会出现乱码在启动配置在VM options中添加-Dfile.encodingUTF-8postman浏览器响应中文数据时显示乱码即使添加了ResponseBody你可能在 Postman 中看到返回的中文变成乱码如??。这是因为StringHttpMessageConverter默认使用的字符集是ISO-8859-1无法正确处理中文。你需要将StringHttpMessageConverter的默认编码改为 UTF-8。在SpringMvcConfig配置文件中添加配置Override public void configureMessageConverters(ListHttpMessageConverter? converters) { //浏览器响应中文数据时显示乱码问题 // 创建 UTF-8 的 StringHttpMessageConverter StringHttpMessageConverter stringConverter new StringHttpMessageConverter(StandardCharsets.UTF_8); converters.add(stringConverter); // 添加 Jackson JSON 转换器 使 Spring MVC 能够处理 JSON 格式的请求和响应 //如果没有此转换器控制器方法返回的对象如 ResponseBody ListUser无法被自动序列化为 JSON或接收 JSON 请求体时会报 415 错误。 MappingJackson2HttpMessageConverter jsonConverter new MappingJackson2HttpMessageConverter(); jsonConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON_UTF8)); converters.add(jsonConverter); }MVC框架传json数据PostMapping(/test5) public ListUser user5(RequestBody ListUser users){ System.out.println(users); return users; } PostMapping(/test6) public User user5(RequestBody User users){ System.out.println(users); return users; }