Jax-rs response.getEntity not working

I want to use javax.ws.rs.core.Response to send and receive a map entity object. But I do not know how to convert the contents back to a map object.

The testCreate () method should execute the create (Card card) method, get the json back and convert it to a card object. But somehow I always get type mismatches, or he says that the getEntity () method cannot be executed as follows: response.getEntity (Card.class).

Does anyone know how I should handle the response correctly in order to convert the returned json object to a Card object again?

Here is my CardResource method:

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

And here is my class 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); } 

And here is my 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; } } 

And here is my class 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; } } 
+4
source share
2 answers

I'm not sure what the right way to name an annotated web services method is the same as a simple method with parameters. Try removing all annotations from your CardResource class and calling this method simply as a class method.

0
source

Is your map class using @XmlRootElement and @XmlElement to enable JAXB JSON mapping to / from POJO?

See an example:

 import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Book { @XmlElement(name = "id") String id; //... 
+1
source

All Articles