gpt4 book ai didi

java - jax-rs response.getEntity 不工作

转载 作者:搜寻专家 更新时间:2023-11-01 03:42:04 29 4
gpt4 key购买 nike

我想使用 javax.ws.rs.core.Response 来发送和接收 Card 实体对象。但我不知道如何将内容转换回 Card 对象。

我的 testCreate() 方法应该执行 create(Card card) 方法,接收回 json 并将其转换为卡片对象。但不知何故,我总是遇到类型不匹配或者它说 getEntity() 方法不能像这样执行:response.getEntity(Card.class)。

有人知道我必须如何正确处理响应,以便我可以再次将返回的 json 实体转换为 Card 对象吗?

这是我的 CardResource 方法:

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response create(Card card) {
Card c = dao.create(card);
if(c.equals(null)) {
return Response.status(Status.BAD_REQUEST).entity("Create failed!").build();
}

return Response.status(Status.OK)
.entity(c)
.type(MediaType.APPLICATION_JSON)
.build();
}

这是我的 CardResourceTests 类

@Test
public void testCreate() {
boolean thrown = false;
CardResource resource = new CardResource();

Card c = new Card(1, "Cardname", "12345678", 1, 1,
"cardname.jpg", new Date(), new Date());

try {
Response result = resource.create(c);
System.out.println(result.getEntity(Card.class)); // not working!!!
if(result.getStatus() != 200) {
thrown = true;
}
} catch(Exception e) {
e.printStackTrace();
thrown = true;
}

assertEquals("Result", false, thrown);
}

这是我的 Card.class

@XmlRootElement
@PersistenceCapable(detachable="true")
public class Card {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;

@Persistent
private Integer id;

@Persistent
private String name;

@Persistent
private String code;

@Persistent
private Integer cardProviderId;

@Persistent
private Integer codeTypeId;

@Persistent
private String picturePath;

@Persistent
private Boolean valid;

@Persistent
private Date mobCreationDate;

@Persistent
private Date mobModificationDate;

@Persistent
private Date creationDate;

@Persistent
private Date modificationDate;

public Card() {
this.setId(null);
this.setName(null);
this.setCode(null);
this.setCardProviderId(null);
this.setCodeTypeId(null);
this.setPicturePath(null);
this.setMobCreationDate(null);
this.setMobModificationDate(null);
this.setCreationDate(null);
this.setModificationDate(null);
}

public Card(Integer id, String name, String code, Integer cardProviderId,
Integer codeTypeId, String picturePath,
Date mobCreationDate, Date mobModificationDate) {
this.setId(id);
this.setName(name);
this.setCode(code);
this.setCardProviderId(cardProviderId);
this.setCodeTypeId(codeTypeId);
this.setPicturePath(picturePath);
this.setMobCreationDate(mobCreationDate);
this.setMobModificationDate(mobModificationDate);
this.setCreationDate(new Date());
this.setModificationDate(new Date());
}

public Key getKey() {
return key;
}

public void setKey(Key key) {
this.key = key;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCode() {
return code;
}

public Integer getCardProviderId() {
return cardProviderId;
}

public void setCardProviderId(Integer cardProviderId) {
this.cardProviderId = cardProviderId;
}

public void setCode(String code) {
this.code = code;
}

public Integer getCodeTypeId() {
return codeTypeId;
}

public void setCodeTypeId(Integer codeTypeId) {
this.codeTypeId = codeTypeId;
}

public String getPicturePath() {
return picturePath;
}

public void setPicturePath(String picturePath) {
this.picturePath = picturePath;
}

public Date getCreationDate() {
return creationDate;
}

public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}

public Date getModificationDate() {
return modificationDate;
}

public void setModificationDate(Date modificationDate) {
this.modificationDate = modificationDate;
}

public Date getMobCreationDate() {
return mobCreationDate;
}

public void setMobCreationDate(Date mobCreationDate) {
this.mobCreationDate = mobCreationDate;
}

public Date getMobModificationDate() {
return mobModificationDate;
}

public void setMobModificationDate(Date mobModificationDate) {
this.mobModificationDate = mobModificationDate;
}

public Boolean getValid() {
return valid;
}

public void setValid(Boolean valid) {
this.valid = valid;
}
}

这是我的 CardDAO 类

public class CardDAO {
private final static Logger logger = Logger.getLogger(CardDAO.class.getName());

public Card create(Card card) {
PersistenceManager pm = PMF.get().getPersistenceManager();

Card c = new Card(card.getId(), card.getName(), card.getCode(),
card.getCardProviderId(), card.getCodeTypeId(), card.getPicturePath(),
new Date(), new Date());

try {
pm.makePersistent(c);
} catch(Exception e) {
logger.severe("Create failed: " + e.getMessage());
return null;
} finally {
pm.close();
}

return c;
}
}

最佳答案

您的 Card 类是否使用 @XmlRootElement@XmlElement 注释来启用 JAXB 将 JSON 映射到/从 POJO?

看例子:

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement
public class Book {
@XmlElement(name = "id")
String id;
//...

关于java - jax-rs response.getEntity 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11960693/

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