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.sprin
2、配置WebInitializer4.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}
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 } }