springmvc json

加入支持的jar:

jackson-core-asl

jackson-mapper-asl


项目pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.lin.project</groupId>
	<artifactId>mybatis</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>mybatis Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.2.7</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.2.2</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>4.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>4.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>4.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>4.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>4.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.8.2</version>
		</dependency>
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>1.1.2</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.32</version>
		</dependency>
		<dependency>
			<groupId>org.quartz-scheduler</groupId>
			<artifactId>quartz</artifactId>
			<version>2.2.1</version>
		</dependency>
		<dependency>
			<groupId>org.quartz-scheduler</groupId>
			<artifactId>quartz-jobs</artifactId>
			<version>2.2.1</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-core-asl</artifactId>
			<version>1.9.13</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-asl</artifactId>
			<version>1.9.13</version>
		</dependency>

	</dependencies>
	<build>
		<finalName>mybatis</finalName>
	</build>
</project>

在springmvc配置文件上,使其能转化json格式输出:

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >    
		<property name="messageConverters">    
			<list>  
				<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />  
			</list>   
		</property>    
	</bean>
整个springmvc配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
	xmlns:mvc="http://www.springframework.org/schema/mvc"  
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
	http://www.springframework.org/schema/context  
	http://www.springframework.org/schema/context/spring-context-3.2.xsd  
	http://www.springframework.org/schema/aop  
	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
	http://www.springframework.org/schema/tx   
	http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
	http://www.springframework.org/schema/mvc   
	http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"  
	>
	
	<!-- HandlerMapping -->  
	<bean  
		class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />  
		
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >    
		<property name="messageConverters">    
			<list>  
				<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />  
			</list>   
		</property>    
	</bean>   
	<!-- HandlerAdapter -->  
	<bean  
		class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />  
	<!-- ViewResolver -->  
	<bean  
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
		<property name="viewClass"  
			value="org.springframework.web.servlet.view.JstlView" />  
		<property name="prefix" value="/WEB-INF/jsp/" />  
		<property name="suffix" value=".jsp" />  
	</bean>  
	<mvc:annotation-driven />  
	<!-- 处理器 -->    
	<!-- <bean name="/hello" class="com.mvc.jn.controller.HelloWorldController"/> -->  
	<context:component-scan base-package="com"/>  
</beans> 

controller类:

package com.lin.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lin.model.StudentEntity;
import com.lin.service.StudentService;

@Controller
@RequestMapping(value = "/")
public class MainController {


	@RequestMapping(value = "/json")
	@ResponseBody
	public Object getJson() {
		List<StudentEntity> list = new ArrayList<StudentEntity>();
		StudentEntity s1 = new StudentEntity("1993-01-28","1","hackcoder","男");
		list.add(s1);
		return list;
	}
}


注意在方法上加@ResponseBody使得方法返回值作为响应内容。

Spring MVC 中,JSON 数据交互通常涉及到控制器层处理HTTP请求并将数据转换为JSON格式返回给前端,或者接收前端发送的JSON数据并进行业务处理。下面是一个简单的 Spring MVC JSON 文件实例: 首先,你需要在Spring MVC配置中启用对JSON的支持,并使用`@ResponseBody`注解将对象直接转换为JSON。 1. **Controller 类**(例如,`UserController.java`): ```java import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @PostMapping("/api/users") public String createUser(@RequestBody User user) { // 对用户进行业务处理 userService.save(user); return "User created successfully"; // 返回一个表示成功的JSON字符串,如{"message": "User created"} } // 其他类似的方法,如更新、删除等 private UserService userService; // 这里假设UserService已经注入 } ``` 2. **User Model** (User.java): ```java import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; public class User { private String name; private int age; // 使用@JsonDeserialize(builder = User.UserBuilder.class) 和 JsonPOJOBuilder 注解创建构建器 @JsonDeserialize(builder = User.UserBuilder.class) @JsonPOJOBuilder(withPrefix = "") static class UserBuilder { // 构建器字段和构造方法省略... } } ``` 3. **XML 或 Java Config 配置**(如果使用XML配置): ```xml <!-- 如果使用XML配置 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json</value> </list> </property> </bean> <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" /> ``` 在这个例子中,当向 `/api/users` 发送一个包含用户信息的JSON POST请求时,Spring MVC会自动将请求体解析为User对象,并通过`createUser`方法处理。响应也是一个JSON字符串,告诉客户端操作是否成功。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值