当使用 GET 方法调用服务时,它可以顺利运行,即打印request.getParameter(“userValue”)。
但在使用 Post 方法时,会为request.getParameter(“userValue”)打印 null 。
HTML code :( jsonObject有效json)
var myData = "userValue=" + jsonObject ;
jQuery.ajax({
type: "POST",
url: "http://localhost:8080/Webservice_JS_26Oct/FieldsToFile/write",
data: myData,
contentType: "application/json; charset=utf-8",
dataType: "json",
Java代码:
@RequestMapping(value = "/FieldsToFile")
public class FileWriter {
@RequestMapping(value = "/write", method = RequestMethod.POST, produces = "application/json")
public String getData(HttpServletRequest request) throws IOException, IllegalStateException, ServletException {
String jsonString = request.getParameter("userValue") ;
System.out.println("jsonString = " + jsonString);
String myData = request.getParameter("myData") ;
我是新手,请告知如何使其适用于POST方法。
答案 0 :(得分:0)
您可以使用request.getInputStream()
打印请求正文部分。我建议您的contentType: "application/json; charset=utf-8",
可以application/x-www-form-urlencoded
或HttpServletRequest request
@Requestbody ..