On a SOAP server, I try to convert Entities class to objects generated by wsimport
.
The objects have the same getters and setters, but I don't now how to inject model's getters into wsimport
object setters
For exemple I need convert this model:
package org.library.webservice.model;
public class User {
private int id;
private String firstname;
private String lastname;
private String email;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
}
to this object :
package org.library.webservice.service.generated.user;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "user", propOrder = {
"email",
"firstname",
"id",
"lastname",
"password"
})
public class User {
protected int id;
protected String firstname;
protected String lastname;
protected String email;
protected String password;
/**
* Obtient la valeur de la propriete email.
*
* @return possible object is
* {@link String }
*/
public String getEmail() {
return email;
}
/**
* Definit la valeur de la propriete email.
*
* @param value allowed object is
* {@link String }
*/
public void setEmail(String value) {
this.email = value;
}
/**
* Obtient la valeur de la propriete firstname.
*
* @return possible object is
* {@link String }
*/
public String getFirstname() {
return firstname;
}
/**
* Definit la valeur de la propriete firstname.
*
* @param value allowed object is
* {@link String }
*/
public void setFirstname(String value) {
this.firstname = value;
}
/**
* Obtient la valeur de la propriete id.
*/
public int getId() {
return id;
}
/**
* Definit la valeur de la propriete id.
*/
public void setId(int value) {
this.id = value;
}
/**
* Obtient la valeur de la propriete lastname.
*
* @return possible object is
* {@link String }
*/
public String getLastname() {
return lastname;
}
/**
* Definit la valeur de la propriete lastname.
*
* @param value allowed object is
* {@link String }
*/
public void setLastname(String value) {
this.lastname = value;
}
/**
* Obtient la valeur de la propriete password.
*
* @return possible object is
* {@link String }
*/
public String getPassword() {
return password;
}
/**
* Definit la valeur de la propriete password.
*
* @param value allowed object is
* {@link String }
*/
public void setPassword(String value) {
this.password = value;
}
}
I think that it's possible with genericity and reflexivity but I do not know how to do.
what you need is to use a Java Bean-Bean mapper like Dozer or MapStruct. I have used MapStruct and it was really easy to setup and use it.
Using MapStruct, this interface has to be created:
@Mapper
public interface UserMapper {
UserMapper INSTANCE = Mappers.getMapper( UserMapper.class );
org.library.webservice.service.generated.user.User toDto(org.library.webservice.model.User user);
org.library.webservice.model.User toEntity(org.library.webservice.service.generated.user.User user);
}
This is all you need to get it work (OK, you have to use maven and add the mapstruct's annotation processor to the plugins, but it is acceptable for not writing java codes for transformations), because the field names are the same in both bean class.
MapStruct will generate the proper implementation and you can check the generated source that it behaves as you expect in the target folder, if not, you can change the way of working by specific annotations or using a decorator class, please check the documentation for it.