spring mvc传入参数不仅仅转换为json,还可以直接将json字符串转换为具体的java对象

时间:2022-01-31 04:03:49

1.controller层

/**
* 查看主播资料
*
* @return
*/
@RequestMapping(value = { "/actor_details" }, method = { RequestMethod.POST })
@ResponseBody
public ResultObject actorDetails(@RequestBody ActorDetailsRequest req) {
logger.debug("查看{}主播信息", req.getAid());
if (req.getAid() == null || req.getAid().equals("null")
|| req.getAid().equals("")) {
return new ResultObject(ResultCode.NULL_PARAM);
}
ActorAndUser result = actorServiceImpl.queryActorData(req.getAid());
return new ResultObject(result);
}

2.java bean对象,必须有set/get方法

/**
* @author LM 本类只存放公共属性,不可实例化
*/
public abstract class CommonRequest implements Serializable { /**
*
*/
private static final long serialVersionUID = 4470502255011343528L; private Integer pageNum; private Integer pageSize; public Integer getPageNum() {
return pageNum;
} public void setPageNum(Integer pageNum) {
this.pageNum = pageNum;
} public Integer getPageSize() {
return pageSize;
} public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
} }
public class ActorDetailsRequest extends CommonRequest {

    /**
*
*/
private static final long serialVersionUID = 6674800202496130064L; private String aid; public String getAid() {
return aid;
} public void setAid(String aid) {
this.aid = aid;
}
}