Fiddler 的post请求有两种格式:
第一种:name1=valule1&name2=value2... 具体写法如图所示:
第二种:json数据格式 {"参数名":"参数值","参数名":"参数值",...} 具体写法如图所示:
代码如下:
package com.cn.webservice.controller;
import java.io.BufferedReader;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cn.webservice.pojo.User;
import com.cn.webservice.service.ILoginService;
@Controller
@RequestMapping("/login")
public class LoginController {
@Resource
private ILoginService loginService;
@RequestMapping("/login1")
@ResponseBody
public String login1(HttpServletRequest request) throws IOException {
System.out.println("this is LoginController");
String userName = request.getParameter("userName");
String passWord = request.getParameter("passWord");
System.out.println(userName + " -- " + passWord);
int key = loginService.login(userName, passWord);
if (key >= 1) {
return "success";
} else {
return "fail";
}
}
@RequestMapping("/login2")
@ResponseBody
public String login2(HttpServletRequest request) throws IOException {
String jsonString = getBodyString(request.getReader());
JSONObject jbJsonObject = new JSONObject().fromObject(jsonString);
User user = (User) JSONObject.toBean(jbJsonObject, User.class);
System.out.println(jbJsonObject);
System.out.println("id:" + user.getId());
System.out.println("name:" + user.getName());
return null;
}
@RequestMapping("/login3")
@ResponseBody
public String login3(User user) throws IOException {
System.out.println("---------------");
System.out.println(user.getUserName());
System.out.println(user.getPassWord());
System.out.println("---------------");
if (true) {
return "success";
} else {
return "fail";
}
}
public String getBodyString(BufferedReader br) {
String inputLine;
String str = "";
try {
while ((inputLine = br.readLine()) != null) {
str += inputLine;
}
br.close();
} catch (IOException e) {
System.out.println("IOException: " + e);
}
return str;
}
}