SPRINGMVC is very powerful, its advantages sometimes like shortcomings, personal opinion;
One time when doing SPRINGMVC, the use of HttpClient sent a POST request error
The request sent by the client was syntactically incorrect.
The information from Baidu is probably the parameter configuration error or something, but not easy to find, the specific error does not output, I want to change the output log level of spring, I use the log4j configuration file, spring default is the Common-log configuration file, this is not in detail
Adding in the log4j configuration file
Log4j.logger.org.springframework=debug,console,file (file is not absolute, mainly see you log4j.rootlogger=debug,console,file and the file here Remain consistent)
You can implement the print parameters in the console, the first represents the log level, the second represents the display in the console, and the third represents the output to a log file
Also attach the code for the httpclient request
Package Com.zyc.json;
Import java.io.IOException;
Import java.io.UnsupportedEncodingException;
Import org.apache.http.HttpEntity;
Import org.apache.http.client.ClientProtocolException;
Import Org.apache.http.client.methods.CloseableHttpResponse;
Import Org.apache.http.client.methods.HttpPost;
Import org.apache.http.entity.StringEntity;
Import org.apache.http.impl.client.CloseableHttpClient;
Import org.apache.http.impl.client.HttpClients;
Import Org.apache.http.util.EntityUtils;
Import Com.alibaba.fastjson.JSONObject; /** * @author: * @date: June 26, 2017 pm 3:15:54 * @description: * @version: * */public class Httpjsonutil {public String Createhttpjson (String Url,jsonobject json) throws Clientprotocolexception, ioexception{closeablehttpclient HT
Tpclient = Httpclients.createdefault ();
String httpstr = null;
HttpPost HttpPost = new HttpPost (URL);
Closeablehttpresponse response = null; Stringentity stringentity = new Stringentity (json.toString (), "UTF-8");//solve Chinese garbled problem stringentity.setcontentencoding ("UTF-8");
Stringentity.setcontenttype ("Application/json");
Httppost.setentity (stringentity);
Response = Httpclient.execute (HttpPost);
httpentity entity = response.getentity ();
Httpstr = entityutils.tostring (Entity, "UTF-8");
if (response! = null) {Entityutils.consume (response.getentity ());
} return HTTPSTR;
}
}
Calling code
User U = new user ();
U.setaccount ("zzz");
U.SETPASSWD ("123456");
Jsonobject json = (jsonobject) Json.tojson (u);
String str = Createhttpjson ("Http://127.0.0.1:6060/RemberBooks/Login.do", JSON);
System.out.println (str);
The code for SPRINGMVC is as follows
Package Com.zyc.controller;
Import Javax.annotation.Resource;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import Org.springframework.stereotype.Controller;
Import Org.springframework.web.bind.annotation.RequestBody;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestParam;
Import Org.springframework.web.bind.annotation.ResponseBody;
Import Com.zyc.entity.LoginUser;
Import Com.zyc.entity.User;
Import Com.zyc.service.LoginService;
Import Com.zyc.util.ResultStatus; /** * @author: * @date: June 23, 2017 morning 9:19:22 * @description: * @version: * * * * @Controller @RequestMapping ("/rember
Books ") public class Logincontroller {private Logger Logger=loggerfactory.getlogger (Logincontroller.class);
@Resource private Loginservice Loginservice; /** * Login Controller * @param loginuser * @return * */@RequestMapping ("/login") @ResponseBody public Resultstatus Login (@RequestBody loginuser loginuseR) {resultstatus result=new resultstatus ();
Loginuser Loginuser=jsonobject.parseobject (JSON, loginuser.class);
Logger.debug (Loginuser.tostring ());
String Data=loginservice.login (Loginuser);
Result.setdata (data);
return result; }/** * Registered controller * @param loginuser * @return */@RequestMapping ("/regiest") @ResponseBody public Result
Status regiest (@RequestBody loginuser loginuser) {resultstatus result=new resultstatus ();
try{String data=loginservice.regiest (loginuser);
Result.setdata (data);
}catch (Exception e) {result.setstatus ("101");
} return result;
}
}