使用Spring MVC +JDBC 实现输出Json数据和视图两种形式 最后面有源码
从web.xml开始配置:
声明定义两个Servlet分别是输出视图和json
rest
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/rest-servlet.xml
1
rest
/rest/*
json
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/json-servlet.xml
json
/json/*
其他配置略过
首先是第一个Servlet – rest,指定的配置文件rest-servlet.xml(默认也是,,,)
在包com.znn.rest.controller下创建一个Controller:UserController.java
用注解@Controller进行声明这是一个Controller,Spring会自动扫描到
package com.znn.rest.controller;
import java.lang.reflect.Method;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.znn.dao.UserDao;
import com.znn.vo.User;
/**
* RestFul 返回视图
*
* @author RANDY.ZHANG
* 2014-5-7
*/
@Controller
public class UserController {
ApplicationContext applicatiOnContext= new ClassPathXmlApplicationContext("beans.xml");
UserDao dao = (UserDao) applicationContext.getBean("userDao");
@RequestMapping("/hi")
public String helloWorld(Model model) {
model.addAttribute("msg", "Hello HelloWorld");
return "hello";
// return "redirect:hello.jsp";
}
@RequestMapping("/user/{id:\\d+}")
public String getUser(Model model,@PathVariable("id") int userid) {
User user;
user = dao.queryAUser(userid);
if (user == null) {
model.addAttribute("tip", "用户不存在!");
}else {
model.addAttribute("tip", user.toString());
}
return "user";
}
@RequestMapping(value = "/user/*",method=RequestMethod.GET)
public String getAllUser(Model model) {
List list = dao.query();
String tipString = "";
for (int i = 0; i ";
}
model.addAttribute("tip",tipString);
return "user";
}
@RequestMapping(value = "/user",method=RequestMethod.GET)
public String getAUser(Model model,
@RequestParam(value="id",required=false,defaultValue="1") int userid,
@RequestHeader("user-agent") String agent) {
User user;
user = dao.queryAUser(userid);
if (user == null) {
model.addAttribute("tip", "用户不存在!");
}else {
model.addAttribute("tip", user.toString());
}
System.out.println(agent);
return "user";
}
}
使用InternalResourceViewResolver进行视图适配,方法返回的字符串会自动添加上前后缀,另外还支持redirect:和forward:两种特殊配置,只是注意下要路径。
关于MVC的注解以及RequestMapping的配置用法到:http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/mvc.html#mvc-introduction,里面讲的挺详细。
第二种输出JSON:
从Spring 3.1开始支持@ResponseBody来直接将数据返回到HTTP响应体重,不在需要ModalView或者其他进行渲染。Spring提供了一些HttpMessageConverter来对数据进行转换,比如StringHttpMessageConverter,FormHttpMessageConverter,MappingJackson2HttpMessageConverter ,其他见这里,这里用到了StringHttpMessageConverter用来进行编码转换,MappingJackson2HttpMessageConverter 用来将数据转为Json形式。
json-servlet.xml
text/html; charset=UTF-8
text/html; charset=UTF-8
从3.1开始推荐使用RequestMappingHandlerMapping和RequestMappingHandlerAdapter来代替DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter。不过不换也木事。。。。
在com.znn.json.controller下新建一个Controller
UserJson.java
package com.znn.json.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.znn.dao.UserDao;
import com.znn.vo.User;
/**
* 返回@ResponseBody需要配置HttpMessageConverters
*
* @author RANDY.ZHANG
* 2014-5-7
*/
@Controller
public class UserJson {
ApplicationContext applicatiOnContext= new ClassPathXmlApplicationContext("beans.xml");
UserDao dao = (UserDao) applicationContext.getBean("userDao");
@RequestMapping("/hi")
public @ResponseBody String helloWorld(Model model) {
// model.addAttribute("msg", "Hello HelloWorld");
return "hello world";
// return "redirect:hello.jsp";
}
@RequestMapping("/user/{id:\\d+}")
public @ResponseBody User getUser(Model model,@PathVariable int id) {
User user;
user = dao.queryAUser(id);
if (user == null) {
model.addAttribute("tip", "用户不存在!");
}else {
model.addAttribute("tip", user.toString());
}
return user;
}
@RequestMapping(value = {"/user/*","/user"},method=RequestMethod.GET)//,produces="application/json" 406
public @ResponseBody List getAllUser(Model model) {
List list = dao.query();
String tipString = "";
for (int i = 0; i ";
}
model.addAttribute("tip",tipString);
return list;
}
@RequestMapping(value = "/user/aaa",method=RequestMethod.GET)
public @ResponseBody List getTest(Model model,HttpServletResponse response, HttpServletRequest request) {
List list = dao.query();
String tipString = "";
for (int i = 0; i ";
}
model.addAttribute("tip",tipString);
return list;
}
}
木有啥好说的….
跟JDBC连接的及DAO层代码和http://www.erdian.net/?p=141是一样的,不再写。
源码:https://github.com/qduningning/SpringAll
spring_restful_json_jdbc,,
spring_restful_json_jdbc