JerseyTest字段在资源中变为NULL

时间:2015-12-19 18:27:53

标签: java jersey

由于某些奇怪的原因,我的实体的字段在我的资源中已更改为NULL。

用户实体
User实体包含:userName,firstName,lastName,email。 JerseyTest实例化一个新的User对象:

newUser = new User();
    newUser.setEmail(getEmail());
    newUser.setFirstName("Erwin");
    newUser.setLastName("Eberhard");
    newUser.setUserName("abc");    

在测试中发送实体
在发送数据之前,测试中的日志告诉我:

[main] INFO feature.AccountStepDefinition - Erwin Eberhard erwin  
      erwineberhard@gmail.com

Everthing似乎没问题,因此测试将数据发送到资源:

logger.info(newUser.toString());
responseUser = target("auth/register").request().post(Entity.json(newUser), User.class); 

AccountResource中
AccountResource检索用户,日志告诉我们:

[qtp1533672820-20] INFO nl.bolt.ffinschrijven.controllers.AccountsController   
- Erwin Eberhard null erwineberhard@gmail.com  

由于某种原因,用户名已在NULL中更改。

AccountResource中的方法

@POST
@Path("/register")
@ApiOperation(value = "Register user.", notes = "After registration a JWT token has been added as header value.")
@ApiResponse(code = 200, message = "", response = User.class, responseHeaders = @ResponseHeader(name = "X-FFI-AUTH", description = "Token generated after authentication", response = String.class) )
@Consumes("application/json")
public Response callback(User user) {

        logger.info(user.toString());

        ServiceResult serviceResult = accountService.register(user);
        if (serviceResult.serviceState == ServiceState.success) {
            String jwt = "Bearer " + JwtHelper.createJWT(UUID.randomUUID().toString(), ApiConfiguration.issuer,
                    user.getEmail(), Long.valueOf(ApiConfiguration.token_ttl));
            return Response.status(201).entity(serviceResult.content).header("Authorization", jwt).build();
        }

        if (serviceResult.serviceState == ServiceState.invalid) {
            return Response.status(400).entity(serviceResult.responseMessages.toString()).build();
        }

        return Response.status(500).build();
}

邮差
当我使用POSTMAN发送数据时,没有问题:

标题

Content-Type application / json

原始内容

{
"firstName": "Erwin",
"lastName" : "Eberhard",
"email" : "erwin@gmail.com",
"userName": "erwineberhard" 
}  

如何用我的JerseyTest获得心爱的201?

更新1

User.class

User类扩展为通用User类:

@Entity
@Table
public class User extends GenericUser {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;

@OneToMany(mappedBy="user", cascade=CascadeType.ALL)
private List<Eventcomponent> eventcomponents =   
new ArrayList<Eventcomponent>();

public Long getId() {
    return id;
}

@Override
public String toString(){
    return this.getFirstName() + " " +  this.getLastName() + " " +     this.getUserName() 
    +  " " + this.getEmail();
}
}

GenericUser.class

@MappedSuperclass
public abstract class GenericUser implements IEmail{

@NotNull(message="Gebruikersnaam niet meegegeven.")
@Column(name="userName", nullable = false)
protected String userName;
@NotNull(message="Email niet meegegeven.")
@Column(name="email", nullable = false)
protected String email;
@NotNull(message="Voornaam niet meegegeven.")
@Column(name="firstName", nullable = false)
protected String firstName;
@NotNull(message="Achternaam niet meegegeven.")
@Column(name="lastName", nullable = false)
protected String lastName;
@Column(name="locked", nullable = false)
protected Boolean locked;

protected String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
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 Boolean getLocked() {
    return locked;
}
public void setLocked(Boolean locked) {
    this.locked = locked;
}  
}

1 个答案:

答案 0 :(得分:1)

看起来protected getUserName()是个问题。序列化程序在序列化时无法找到userName属性,因此该属性名称的JSON中没有字段。当服务器获取JSON时,没有"userName"字段,因此它在服务器端对象中保持为null。要解决此问题,只需制作方法public