Java jax rs response.getEntity不工作

Java jax rs response.getEntity不工作,java,jersey,jax-rs,Java,Jersey,Jax Rs,我想使用javax.ws.rs.core.Response发送和接收一个卡实体对象。但我不知道如何将内容转换回卡片对象 我的testCreate()方法应该执行create(Card Card)方法,接收回json并将其转换为Card对象。但是我总是得到类型不匹配,或者它说getEntity()方法不能像这样执行:response.getEntity(Card.class) 有人知道我必须如何正确处理响应,以便将返回的json实体再次转换为Card对象吗 下面是我的CardResource方法:

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

我的testCreate()方法应该执行create(Card Card)方法,接收回json并将其转换为Card对象。但是我总是得到类型不匹配,或者它说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();
}
这是我的课程

@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);
}
这是我的名片

@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;
}
}
这是我的卡片课

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或从POJO映射JSON

见示例:

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


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

我不确定像调用带参数的简单方法一样调用带注释的web服务方法是否正确。尝试从您的
CardResource
类中删除所有注释,并像调用class方法一样调用此方法。

我刚刚添加了我的card类。是否所有属性都必须使用@XmlElement进行注释?是的,@XmlElement用于帮助JAXB填充类属性。您是想像测试简单方法还是服务一样测试它?我将它测试为简单方法调用,而不是服务。没有涉及web容器。我已经删除了注释,但它没有改变任何内容。错误消息是:“类型响应中的getEntity方法不适用于以下代码行中的参数(类)”:System.out.println(result.getEntity(Card.Class));。getEntity似乎返回了一个对象,当我调试时,我看到该对象充满了卡片项,但我无法将其转换为卡片对象。我认为我使用getEntity的方式是错误的。但我不知道它应该如何工作;然后我得到以下输出:com.acolsolutions.cardkeeper.server.entities。Card@20d9896e.thanks感谢你迄今为止的帮助。我很感激。我仍然无法解决此问题。请尝试将
toString()
方法添加到
Card
类中,以便正确打印它。已尝试,但它仅打印出:com.acolsolutions.cardkeeper.server.entities。Card@172fb0af