所有我都是春季靴子的新手,并尝试做一些新的。目前我有3个pojo类,用于扩展用户类中的#。我试图在所有类的setter id方法中分配may @PathVariable partnerid的值,如下所示。 我的问题是,Spring boot是否提供了任何触发我的变量到我的Partner类的@annotation。 我试图通过在setter方法中添加合作伙伴ID来更新我的用户请求。
@RestController
@RequestMapping(value = "/push/fire/")
public class PushIdController {
private final static Logger log = LoggerFactory.getLogger(PushIdController.class);
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
@RequestMapping(value = "/partner/{partnerId}", method = RequestMethod.POST)
@annotation("new Partner().setPartnerId("PathVariable value")")
public User userLogin(@PathVariable("partnerId") String partnerId, @RequestBody User user){
return user;
}
}
合作伙伴类
public class Partner {
private String partnerId;
public String getPartnerId() { return partnerId; }
public void setPartnerId(String partnerId) {
this.partnerId = partnerId;
}
@Override
public String toString() {
return "Partner{" + "partnerId='" + partnerId + '\'' + '}';}
}
用户类
public class User {
private final static Logger log = LoggerFactory.getLogger(User.class);
private String userId;
private Role role;
private Video video;
public void setUserId(String userId) {
log.info("set-User-id");
// getPartnerId null
this.userId = userId + "_" + getPartnerId() ;
}
public void setRole(Role role) {
log.info("set-User-Role");
this.role = role;
}
public void setVideo(Video video) {
log.info("set-User-Video");
this.video = video;
}
}
角色类
public class Role {
private String roleId;
public void setRoleId(String roleId) {
// getPartnerId null
this.roleId = roleId + "_" + getPartnerId();
}
}
视频课程
public class Video {
private String videoId;
public void setVideoId(String videoId) {
this.videoId = videoId + "_" + getPartnerId();
}
}
Json请求: -
{
"userId": "1d3ejjj",
"userName": "Nabeel.amd93@gmail.com",
"phone": "03153817177",
"role": {
"roleId": "d423",
"name": "nabeel ahmed"
},
"video": {
"videoId": "123456d",
"h": "25",
"w": "25"
}
}
Json回应: -
{
"userId": "1d3ejjj_1d2d6dq0jhjhk",
"userName": "Nabeel.amd93@gmail.com",
"phone": "03153817177",
"role": {
"roleId": "d423_1d2d6dq0jhjhk",
"name": "nabeel ahmed"
},
"video": {
"videoId": "123456d_1d2d6dq0jhjhk",
"h": "25",
"w": "25"
}
}
正如我们所知,当我们点击请求时Json在方法调用期间分配到Pojo调用setter方法我只想更新每个类的id。 我想重新更新,如下所示
user.setUserId(user.getUserId+"_"+partnerId);
user.getVideo().setVideoId(user.getVideo().getVideoId+"_"+partnerId);
和soo .....
答案 0 :(得分:0)
All经过大量搜索后,我找到了将路径变量触发到Pojo类的方法。
@annotation调用“InitBinder”
Spring提供了@InitBinder注释,用于标识初始化WebDataBinder的方法,此WebDataBinder填充注释方法的参数。因此,简单的方法是在Spring中注册一些自定义编辑器。所以我通过获取请求参数的值来在方法中创建我的伙伴对象。
注意: - 当我在顶部发帖时,我忘了添加带有变量partnerId的静态关键字
合作伙伴课程
public class Partner {
public static String partnerId;
public static String getPartnerId() { return partnerId; }
public void setPartnerId(String partnerId) {
this.partnerId = partnerId;
}
@Override
public String toString() {
return "Partner{" + "partnerId='" + partnerId + '\'' + '}';}
}
}
数据后我在我的控制器中添加此方法,如
@InitBinder
public void initBinder(final WebDataBinder webdataBinder) {
webdataBinder.registerCustomEditor(String.class, new PartnerFireId());
}
和PartnerFireId类一样
public class PartnerFireId extends PropertyEditorSupport {
private Partner partner;
public void setAsText(final String partnerKey) throws IllegalArgumentException {
partner = new Partner();
partner.setPartnerId(partnerKey);
}
}