如何通过JavaConfig配置SpringMVC接收并返回JSON数据?

更新于
2026-10-12 09:22:30
1阅读来源:SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计913个文字,预计阅读时间需要4分钟。

如何通过JavaConfig配置SpringMVC接收并返回JSON数据?

javaimport org.springframework.web.bind.annotation.RestController;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.http.ResponseEntity;import org.springframework.http.HttpStatus;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.PutMapping;import org.springframework.web.bind.annotation.DeleteMapping;import org.springframework.web.bind.annotation.PatchMapping;import org.springframework.web.bind.annotation.CrossOrigin;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.ResponseStatus;import org.springframework.web.bind.annotation.Validated;import org.springframework.validation.annotation.Validated;import org.springframework.validation.BindingResult;import org.springframework.validation.FieldError;import org.springframework.web.bind.MethodArgumentNotValidException;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RequestHeader;import org.springframework.web.bind.annotation.ResponseHeader;import org.springframework.web.bind.annotation.Localized;import org.springframework.web.bind.annotation.RequestAttribute;import org.springframework.web.bind.annotation.InitBinder;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.SessionAttributes;import org.springframework.web.bind.annotation.CookieValue;import org.springframework.web.bind.annotation.MatrixVariable;import org.springframework.web.bind.annotation.QueryParam;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.PutMapping;import org.springframework.web.bind.annotation.DeleteMapping;import org.springframework.web.bind.annotation.PatchMapping;import org.springframework.web.bind.annotation.CrossOrigin;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.ResponseStatus;import org.springframework.web.bind.annotation.Validated;import org.springframework.validation.annotation.Validated;import org.springframework.validation.BindingResult;import org.springframework.validation.FieldError;import org.springframework.web.bind.MethodArgumentNotValidException;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RequestHeader;import org.springframework.web.bind.annotation.ResponseHeader;import org.springframework.web.bind.annotation.Localized;import org.springframework.web.bind.annotation.RequestAttribute;import org.springframework.web.bind.annotation.InitBinder;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.SessionAttributes;import org.springframework.web.bind.annotation.CookieValue;import org.springframework.web.bind.annotation.MatrixVariable;import org.springframework.web.bind.annotation.QueryParam;import org.springframework.web.bind.annotation.PathVariable;

如何通过JavaConfig配置SpringMVC接收并返回JSON数据?

1、导包,spring的相关包,和JSON的包,解析JSON格式spring用到了JSON的包

4.2.4.RELEASE 2.5.1 org.springframework spring-webmvc ${springframework.version} org.springframework spring-web ${springframework.version} org.springframework spring-jdbc ${springframework.version} org.springframework spring-tx ${springframework.version} org.springframework spring-context-support ${springframework.version} org.springframework spring-aop ${springframework.version} org.springframework spring-aspects ${springframework.version} org.springframework spring-beans ${springframework.version} org.springframework spring-core ${springframework.version} org.springframework spring-context ${springframework.version} com.fasterxml.jackson.core jackson-databind ${jackson.version} com.fasterxml.jackson.core jackson-core ${jackson.version} com.fasterxml.jackson.core jackson-annotations ${jackson.version} 2、配置WebInitializer

package com.amiu.spring.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{ @Override protected Class [] getRootConfigClasses() { return new Class[]{MvcConfig.class}; } @Override protected Class [] getServletConfigClasses() { return null; } @Override protected String[] getServletMappings() { return new String[]{"/"}; } } 3、这里是重点,配置MvcConfig

package com.amiu.spring.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ViewResolverRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.json.MappingJackson2JsonView; @Configuration @EnableWebMvc @ComponentScan("com.amiu") public class MvcConfig extends WebMvcConfigurerAdapter { @Override public void configureDefaultServletHandling( DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Override public void configureViewResolvers(ViewResolverRegistry registry) { //registry.jsp("/WEB-INF/page/", ".jsp"); //将返回的参数转换为JSON,MappingJackson2JsonView就是做这个的 registry.enableContentNegotiation(new MappingJackson2JsonView()); } @Override public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { configurer.favorPathExtension(true) .defaultContentType(MediaType.APPLICATION_JSON)//默认接收JSON .mediaType("json", MediaType.APPLICATION_JSON); } } 4、Controller

package com.amiu.spring; 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.RestController; import com.amiu.Bean; @Controller public class SpringController { //接收JSON加上@RequestBody,这里一定用一个对象来接收,不能直接接收单个字段 @RequestMapping("/testJson") public Bean testJson(@RequestBody Bean bean){ System.out.println("in method"); //从页面仅仅获取id,返回我们new的一个Bean return new Bean(bean.getId(), "json", "{jsonPwd}", 13930000); } } 5、传给spring的JSON的格式

传给spring: { "id":1, "name":"", "password":"", "phone":"" } spring返回: { "bean": { "id": 0, "name": "json", "password": "{jsonPwd}", "phone": 13930000 } }

本文共计913个文字,预计阅读时间需要4分钟。

如何通过JavaConfig配置SpringMVC接收并返回JSON数据?

javaimport org.springframework.web.bind.annotation.RestController;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.http.ResponseEntity;import org.springframework.http.HttpStatus;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.PutMapping;import org.springframework.web.bind.annotation.DeleteMapping;import org.springframework.web.bind.annotation.PatchMapping;import org.springframework.web.bind.annotation.CrossOrigin;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.ResponseStatus;import org.springframework.web.bind.annotation.Validated;import org.springframework.validation.annotation.Validated;import org.springframework.validation.BindingResult;import org.springframework.validation.FieldError;import org.springframework.web.bind.MethodArgumentNotValidException;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RequestHeader;import org.springframework.web.bind.annotation.ResponseHeader;import org.springframework.web.bind.annotation.Localized;import org.springframework.web.bind.annotation.RequestAttribute;import org.springframework.web.bind.annotation.InitBinder;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.SessionAttributes;import org.springframework.web.bind.annotation.CookieValue;import org.springframework.web.bind.annotation.MatrixVariable;import org.springframework.web.bind.annotation.QueryParam;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.PutMapping;import org.springframework.web.bind.annotation.DeleteMapping;import org.springframework.web.bind.annotation.PatchMapping;import org.springframework.web.bind.annotation.CrossOrigin;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.ResponseStatus;import org.springframework.web.bind.annotation.Validated;import org.springframework.validation.annotation.Validated;import org.springframework.validation.BindingResult;import org.springframework.validation.FieldError;import org.springframework.web.bind.MethodArgumentNotValidException;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RequestHeader;import org.springframework.web.bind.annotation.ResponseHeader;import org.springframework.web.bind.annotation.Localized;import org.springframework.web.bind.annotation.RequestAttribute;import org.springframework.web.bind.annotation.InitBinder;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.SessionAttributes;import org.springframework.web.bind.annotation.CookieValue;import org.springframework.web.bind.annotation.MatrixVariable;import org.springframework.web.bind.annotation.QueryParam;import org.springframework.web.bind.annotation.PathVariable;

如何通过JavaConfig配置SpringMVC接收并返回JSON数据?

1、导包,spring的相关包,和JSON的包,解析JSON格式spring用到了JSON的包

4.2.4.RELEASE 2.5.1 org.springframework spring-webmvc ${springframework.version} org.springframework spring-web ${springframework.version} org.springframework spring-jdbc ${springframework.version} org.springframework spring-tx ${springframework.version} org.springframework spring-context-support ${springframework.version} org.springframework spring-aop ${springframework.version} org.springframework spring-aspects ${springframework.version} org.springframework spring-beans ${springframework.version} org.springframework spring-core ${springframework.version} org.springframework spring-context ${springframework.version} com.fasterxml.jackson.core jackson-databind ${jackson.version} com.fasterxml.jackson.core jackson-core ${jackson.version} com.fasterxml.jackson.core jackson-annotations ${jackson.version} 2、配置WebInitializer

package com.amiu.spring.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{ @Override protected Class [] getRootConfigClasses() { return new Class[]{MvcConfig.class}; } @Override protected Class [] getServletConfigClasses() { return null; } @Override protected String[] getServletMappings() { return new String[]{"/"}; } } 3、这里是重点,配置MvcConfig

package com.amiu.spring.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ViewResolverRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.json.MappingJackson2JsonView; @Configuration @EnableWebMvc @ComponentScan("com.amiu") public class MvcConfig extends WebMvcConfigurerAdapter { @Override public void configureDefaultServletHandling( DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Override public void configureViewResolvers(ViewResolverRegistry registry) { //registry.jsp("/WEB-INF/page/", ".jsp"); //将返回的参数转换为JSON,MappingJackson2JsonView就是做这个的 registry.enableContentNegotiation(new MappingJackson2JsonView()); } @Override public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { configurer.favorPathExtension(true) .defaultContentType(MediaType.APPLICATION_JSON)//默认接收JSON .mediaType("json", MediaType.APPLICATION_JSON); } } 4、Controller

package com.amiu.spring; 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.RestController; import com.amiu.Bean; @Controller public class SpringController { //接收JSON加上@RequestBody,这里一定用一个对象来接收,不能直接接收单个字段 @RequestMapping("/testJson") public Bean testJson(@RequestBody Bean bean){ System.out.println("in method"); //从页面仅仅获取id,返回我们new的一个Bean return new Bean(bean.getId(), "json", "{jsonPwd}", 13930000); } } 5、传给spring的JSON的格式

传给spring: { "id":1, "name":"", "password":"", "phone":"" } spring返回: { "bean": { "id": 0, "name": "json", "password": "{jsonPwd}", "phone": 13930000 } }