阅读背景:

Spring 4MVC在控制层传回JSON字符串

来源:互联网 

直接上代码:


控制层:

package package1;

import java.util.ArrayList;

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;

/**
 * 控制层以response响应的方式返回JSON对象
 * @author acer
 * 控制层通过consumes="application/json"限制前台传递过来的数据必须是json格式的数据
 * 通过produces="application/json"设置返回的userinfo中的数据转换成json对象并回传给客户端
 * @ResponseBody指的是将json字符串作为响应处理
 */
@Controller
public class CreateJSON {

	@RequestMapping(value="createJSONURL",method=RequestMethod.POST,consumes="application/json",
			produces="application/json")
	@ResponseBody
	public Userinfo createJSON(){
		
		Userinfo userinfo = new Userinfo();
		userinfo.setUsername("欧阳川");
		userinfo.setPassword("ouyangchuan");
		userinfo.setStudyList(new ArrayList<String>());
		userinfo.getStudyList().add("Java");
		userinfo.getStudyList().add("c语言");
		return userinfo;
	}
}
package package1;

import j



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: