Spring REST服务Controller多个POJO参数

LPProd 发布于 2015/05/29 15:12
阅读 3K+
收藏 1

有木有一个大神帮忙指导下问题,最近准备使用Spring提供的REST,这是我的服务端:

@RestController
@RequestMapping("/rest/user")
public class RestUsersController {

@RequestMapping(value="/createUser", method=RequestMethod.POST)
public String createUser(@RequestBody UserDetail userDetail){
System.out.println("创建用户:"+userDetail.toString());
return "创建用户成功";
}

@RequestMapping(value="/newUser", method=RequestMethod.POST)
public String newUser(UserDetail userDetail,RoleDetail roleDetail){
System.out.println("新增用户:"+userDetail.toString());
System.out.println("新增角色:"+roleDetail.toString());
return "新增用户成功";
}

@RequestMapping(value="/saveUser", method=RequestMethod.POST)
public String saveUser(@RequestParam(value="userDetail", required=false) UserDetail userDetail){
System.out.println("保存用户:"+userDetail.toString());
return "保存用户成功";
}


我的客户端:
String url = "http://localhost:8091/listlessp-restful-demo/rest/user/createUser";

long responseLength = 0;//响应长度
String responseContent = "";//响应内容

UserDetail userDetail = new UserDetail();
userDetail.setEmailId("zhangmou@revenco.com");
userDetail.setUserName("张某");
JSONObject jsonObj = JSONObject.fromObject(userDetail);
System.out.println("Object ==> Json:"+jsonObj.toString());

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Accept", "application/json;charset=utf-8");

try {
//设置字符集
StringEntity entity = new StringEntity(jsonObj.toString(),"UTF-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
       httpPost.setEntity(entity);
       
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);

System.out.println("状态:"+httpResponse.getStatusLine().getStatusCode());
HttpEntity responseEntity = httpResponse.getEntity();//获取响应实体
if (null != responseEntity) {
responseLength = responseEntity.getContentLength();
responseContent = EntityUtils.toString(responseEntity, "UTF-8");
EntityUtils.consume(responseEntity); //Consume response content
}
System.out.println("请求地址: " + httpPost.getURI());
System.out.println("响应状态: " + httpResponse.getStatusLine());
System.out.println("响应长度: " + responseLength);
System.out.println("响应内容: " + responseContent);
} catch (ConnectTimeoutException e) {
System.out.println("请求通信[" + url + "]时连接超时,堆栈轨迹如下:\n"+e);
} catch (SocketTimeoutException e) {
System.out.println("请求通信[" + url + "]时读取超时,堆栈轨迹如下:\n"+e);
} catch (Exception e) {
System.out.println("POST请求出现异常:\n"+e);
}
System.out.println("**********  Rest返回值:\n"+responseContent);

上面客户端的代码可以成功请求服务端的“createUser”方法,我想请求服务端的“newUser”方法该如何写客户端,还有我服务端的“newUser”是否有问题。请大神指点下,谢谢。

加载中
0
南北024
南北024
@RequestMapping(value="/newUser", method=RequestMethod.POST)
public String newUser(UserDetail userDetail,RoleDetail roleDetail){
System.out.println("新增用户:"+userDetail.toString());
System.out.println("新增角色:"+roleDetail.toString());
return "新增用户成功";

}

这样直接定义参数  是匹配不到的

看一下spring的官方文档  有很详细的说明

http://docs.spring.io/spring/docs/4.2.0.RC1/spring-framework-reference/htmlsingle/#mvc-features

返回顶部
顶部