SpringMVC JavaConfig如何接收JSON并返回JSON?
- 内容介绍
- 文章标签
- 相关推荐
本文共计603个文字,预计阅读时间需要3分钟。
plaintext
1.引入Spring相关包和JSON处理包
2.使用Spring Web MVC和Spring Web模块
3.解析JSON格式
4.Spring利用JSON包进行数据交换
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分钟。
plaintext
1.引入Spring相关包和JSON处理包
2.使用Spring Web MVC和Spring Web模块
3.解析JSON格式
4.Spring利用JSON包进行数据交换
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 } }

