gpt4 book ai didi

java - JerseyTest 字段在资源中变为 NULL

转载 作者:行者123 更新时间:2023-11-30 10:52:21 25 4
gpt4 key购买 nike

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

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

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

似乎一切正常,所以测试将数据发送到资源:

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

账户资源
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
当我用 POSTMAN 发送数据时,没有问题:

标题

内容类型 application/json

原始内容

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

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

更新 1

用户类

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;
}
}

最佳答案

看起来 protected getUserName() 是问题所在。序列化器在序列化时找不到 userName 属性,所以在该属性名的 JSON 中没有字段,当它消失时。当服务器获取 JSON 时,没有 "userName" 字段,因此它在服务器端对象中保持为空。要修复它,只需将方法设置为public

关于java - JerseyTest 字段在资源中变为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34373977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com