1.jar包
jackson-annotations2.9.8
jackson-core.2.9.8
jackson-databind.2.9.8
2.web.xml配置
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置spring-mvc.xm的路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3.项目目录
4.这里运用的ssm框架,service和dao层就不说了
主要是controller
package per.czt.restful.web.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
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.ResponseBody;
import per.czt.restful.domain.User;
import per.czt.restful.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/listAll/{id}/{username}")
public @ResponseBody List<User> listAllUser(Model model,@PathVariable("id")Integer id,@PathVariable("username")String username)
{
Map<String,Object> map=new HashMap<String,Object>();
map.put("id", id);
map.put("username",username);
List<User> userList=userService.searchUser(map);
model.addAttribute("userList", userList);
for(User u:userList)
{
System.out.println("id:"+u.getId());
System.out.println("username:"+u.getUsername());
System.out.println("password:"+u.getPassword());
System.out.println("email:"+u.getEmail());
}
//这里由responseBody响应直接返回json字符串
return userList;
}
@RequestMapping("/listAll2/{id}/{username}")
public String listAllUser2(Model model,@PathVariable("id")Integer id,@PathVariable("username")String username)
{
Map<String,Object> map=new HashMap<String,Object>();
map.put("id", id);
map.put("username",username);
List<User> userList=userService.searchUser(map);
model.addAttribute("userList", userList);
for(User u:userList)
{
System.out.println("id:"+u.getId());
System.out.println("username:"+u.getUsername());
System.out.println("password:"+u.getPassword());
System.out.println("email:"+u.getEmail());
}
return "user/userList";
}
}
在浏览器输入http://localhost:8080/restful/user/listAll/2/22 ,观察到json字符串
5.view层
jsp/user/userList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1">
<tr><td>id</td><td>username</td><td>password</td><td>email</td></tr>
<c:forEach items="${userList }" var="user">
<tr><td>${user.id }</td><td>${user.username }</td><td>${user.password }</td><td>${user.email }</td></tr>
</c:forEach>
</table>
</body>
</htm
在浏览器输入http://localhost:8080/restful/user/listAll2/2/22 ,同样观察到了id为2 且username为22的user