jax-rs response.getEntity无效

时间:2012-08-14 21:03:11

标签: java jersey jax-rs

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

我的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();
}

这是我的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;
}
}

2 个答案:

答案 0 :(得分:1)

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

参见示例:

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


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

答案 1 :(得分:0)

我不确定将带注释的Web服务方法称为带params的简单方法是否正确。尝试从CardResource类中删除所有注释,并像调用类方法一样调用此方法。