springmvc 很强大,它的优点有时候就像缺点一样,个人见解;
一次在做springmvc时,使用httpclient 发了个post 请求出现错误
The request sent by the client was syntactically incorrect.
从百度上查到的信息大概就是参数配置错误什么的,但是不好找,具体的错误没有输出,就想改变spring的输出日志级别,我使用的是log4j配置文件,spring默认的是common-log配置文件,这里不在细说
在log4j 配置文件中增加
log4j.logger.org.springframework=debug,Console,File(File 并不是绝对的,主要看你log4j.rootLogger=DEBUG,Console,File 和这里的File 保持一致)
就可以实现在控制台打印参数,第一个代表日志级别,第二个代表在控制台显示,第三个 代表输出到日志文件
另附上httpclient 请求的代码
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:2017年6月26日 下午3:15:54
* @description:
* @version :
*
*/
public class HttpJsonUtil {
public String createHttpJson(String url,JSONObject json ) throws ClientProtocolException, IOException{
CloseableHttpClient httpClient = HttpClients.createDefault();
String httpStr = null;
HttpPost httpPost = new HttpPost(url);
CloseableHttpResponse response = null;
StringEntity stringEntity = new StringEntity(json.toString(),"UTF-8");//解决中文乱码问题
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;
}
}
调用代码
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);
springmvc 的代码如下
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:2017年6月23日 上午9:19:22
* @description:
* @version :
*
*/
@Controller
@RequestMapping("/RemberBooks")
public class LoginController {
private Logger logger=LoggerFactory.getLogger(LoginController.class);
@Resource
private LoginService loginService;
/**
* 登录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;
}
/**
* 注册Controller
* @param loginUser
* @return
*/
@RequestMapping("/Regiest")
@ResponseBody
public ResultStatus 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;
}
}