I am making application using JAVA EE,JAX-RS,JPA,GLASSFISH. Response is working properly in case of MediaType.APPLICATION_XML. It's not working in MediaType.APPLICATION_JSON.
Here is my pojo class
@Entity
@XmlRootElement
public class Book {
private int id;
private String name;
private String publication;
private String price;
@Id
@Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Basic
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic
@Column(name = "publication")
public String getPublication() {
return publication;
}
public void setPublication(String publication) {
this.publication = publication;
}
@Basic
@Column(name = "price")
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Book book = (Book) o;
if (id != book.id) return false;
if (name != null ? !name.equals(book.name) : book.name != null) return false;
if (publication != null ? !publication.equals(book.publication) : book.publication != null) return false;
if (price != null ? !price.equals(book.price) : book.price != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (publication != null ? publication.hashCode() : 0);
result = 31 * result + (price != null ? price.hashCode() : 0);
return result;
}
}
Here is the pom.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.rajesh</groupId>
<artifactId>Library</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
Resource class for endpoint
@Path("books")
public class BookResource {
@GET
@Path("{bookId}")
@Produces({MediaType.APPLICATION_JSON})
public Response getBookById(@PathParam("bookId") int id) {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("abc");
EntityManager entityManager = entityManagerFactory.createEntityManager();
Book book = entityManager.find(Book.class, id);
if (book == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
return Response.status(Response.Status.OK).entity(book).build();
}
}
This is a bug in Glassfish: https://github.com/javaee/glassfish/issues/21440 It probably won't be fixed, you should switch to http://www.payara.fish/ which is patched and updated version of Glassfish (has even commercial support if needed).
This bug is fixed in Payara.