SpringMVC JavaConfig如何接收JSON并返回JSON?

2026-04-15 09:309阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

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

SpringMVC JavaConfig如何接收JSON并返回JSON?

plaintext

1.引入Spring相关包和JSON处理包

2.使用Spring Web MVC和Spring Web模块

3.解析JSON格式

4.Spring利用JSON包进行数据交换

SpringMVC JavaConfig如何接收JSON并返回JSON?

版本信息: - Spring版本:4.2.4.RELEASE - JSON包版本:2.5.1 - 包名:org.springframework, org.springframework.webmvc, ${springframework.version}, org.springframework.web, ${springframework.version}, org.sprin

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 } }

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

SpringMVC JavaConfig如何接收JSON并返回JSON?

plaintext

1.引入Spring相关包和JSON处理包

2.使用Spring Web MVC和Spring Web模块

3.解析JSON格式

4.Spring利用JSON包进行数据交换

SpringMVC JavaConfig如何接收JSON并返回JSON?

版本信息: - Spring版本:4.2.4.RELEASE - JSON包版本:2.5.1 - 包名:org.springframework, org.springframework.webmvc, ${springframework.version}, org.springframework.web, ${springframework.version}, org.sprin

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 } }