I have a little RESTful WebService running on Wildfly 8. Testing @GET works perfectly fine, but @POST won't accept my JSON object. I am having trouble finding out what is wrong in the code and I would appreciate help very much! Where is the error in the code?
The Report Entity:
@Entity
@XmlRootElement
public class Report implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@XmlAttribute
private Long id;
@NotNull
private String reportContent;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getReportContent() {
return reportContent;
}
public void setReportContent(String reportContent) {
this.reportContent = reportContent;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Report)) {
return false;
}
Report other = (Report) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.myprog.abc.domain.Report[ id=" + id + " ]";
}
}
The Report Resource:
@Path("report")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class ReportResource {
@EJB
private ReportService rs;
@GET
public Response findReports() {
final List<Report> reports = rs.findAllReports();
if(reports.size() <= 0)
throw new NotFoundException("No reports found.");
return Response.ok(new GenericEntity<List<Report>>(reports) {})
.build();
}
@GET
@Path("{id:[1-9][0-9]*}")
public Response findReport(@PathParam("id") Long id) {
Report report = rs.findReport(id);
if(report == null)
throw new NotFoundException("No report found having the id " + id + ".");
return Response.ok(report)
.build();
}
@POST
public Response createReport(@Valid Report report) {
rs.saveReport(report);
return Response.created(URI.create("/" + report.getId()))
.build();
}
}
When testing @GET using http://localhost:8080/abc/rest/report/123
the server returns:
{"reportContent":"This is my Report test content!","id":123}
When testing @POST using http://localhost:8080/abc/rest/report
and {"reportContent":"This is my Report test content!"} the server returns:
Status Code: 415 Unsupported Media Type
Connection: keep-alive
Content-Length: 0
Date: Tue, 07 Oct 2014 14:04:55 GMT
Server: WildFly/8
x-powered-by: Undertow/1