ssm中利用ResponseBody註解返回json資料
阿新 • • 發佈:2018-12-19
- 在pom.xm新增依賴
<dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.12</version> </dependency>
- 在spring-mvc的配置檔案中新增jackson反序列化的配置
<mvc:annotation-driven> <mvc:message-converters register-defaults="false"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!--SpringMVC jakson反序列化的配置--> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
- 在控制器的函式中,新增@ResponseBody註解
- 返回的型別中必須有setter和getter方法