Java RESTful Web服务不接受@POST?

Java RESTful Web服务不接受@POST?,java,json,web-services,rest,wildfly,Java,Json,Web Services,Rest,Wildfly,我在Wildfly 8上运行了一个小的RESTful Web服务。 测试@GET工作得非常好,但是@POST不会接受我的JSON对象。 我很难找出代码中的错误,我将非常感谢您的帮助!代码中的错误在哪里 报告实体: @Entity @XmlRootElement public class Report implements Serializable { private static final long serialVersionUID = 1L; @Id @Generat

我在Wildfly 8上运行了一个小的RESTful Web服务。 测试@GET工作得非常好,但是@POST不会接受我的JSON对象。 我很难找出代码中的错误,我将非常感谢您的帮助!代码中的错误在哪里

报告实体:

@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 + " ]";
    }
}
报告资源:

@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();
    }
}
使用
http://localhost:8080/abc/rest/report
和{“reportContent”:“这是我的报告测试内容!”}服务器返回:

{"reportContent":"This is my Report test content!","id":123}
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

确保您的请求中有标题
内容类型
。该值必须是
application/json
,因为这是您期望的值


我很肯定你错过了

确保您的请求中有标题
内容类型
。该值必须是
application/json
,因为这是您期望的值


我很肯定你错过了

我使用jaxrs客户端api测试了您的示例:

Client client = ClientBuilder.newClient();
    WebTarget myResource = client.target("http://localhost:8080/web/rest/report");
    Report m = new Report();
    m.setId((long) 12);
    m.setReportContent("asdf");
    Entity<Report> report = Entity.json(m);
    Response r = myResource.request(MediaType.APPLICATION_JSON).post(report);
    System.out.println(r.getHeaderString("Location"));
Client-Client=ClientBuilder.newClient();
WebTarget myResource=client.target(“http://localhost:8080/web/rest/report");
报告m=新报告();
m、 setId((长)12);
m、 setReportContent(“asdf”);
实体报告=Entity.json(m);
Response r=myResource.request(MediaType.APPLICATION_JSON).post(report);
System.out.println(r.getHeaderString(“位置”);

结束它在响应位置头中为我提供属性值。

我使用jaxrs客户端api测试了您的示例:

Client client = ClientBuilder.newClient();
    WebTarget myResource = client.target("http://localhost:8080/web/rest/report");
    Report m = new Report();
    m.setId((long) 12);
    m.setReportContent("asdf");
    Entity<Report> report = Entity.json(m);
    Response r = myResource.request(MediaType.APPLICATION_JSON).post(report);
    System.out.println(r.getHeaderString("Location"));
Client-Client=ClientBuilder.newClient();
WebTarget myResource=client.target(“http://localhost:8080/web/rest/report");
报告m=新报告();
m、 setId((长)12);
m、 setReportContent(“asdf”);
实体报告=Entity.json(m);
Response r=myResource.request(MediaType.APPLICATION_JSON).post(report);
System.out.println(r.getHeaderString(“位置”);

结束它在响应位置标题中为我提供属性值。

我的问题是,为什么需要post?有没有什么特别需要的帖子是你用get无法做到的?我知道,对于我的JSON数据,我一直在使用GET,尤其是因为我看到的是您正在请求NG数据,您是否真的在修改任何内容?事实上,这里有一个链接可以帮助您解决设置标头时遇到的一些问题。因此,代码是完全正确的。虽然我不明白为什么我应该把所有的数据放在一个GET行中,而不是干净地隐藏在POST中。我确实理解这两种方法都有效,但你会从哪里看到GET的优势呢?首先,POST还有其他用途。POST可以用于实际编辑数据,而GET只是获取数据,仅此而已。在我看来,更好的做法是使用你正在做的行动的路线。我的问题是,你为什么需要发帖?有没有什么特别需要的帖子是你用get无法做到的?我知道,对于我的JSON数据,我一直在使用GET,尤其是因为我看到的是您正在请求NG数据,您是否真的在修改任何内容?事实上,这里有一个链接可以帮助您解决设置标头时遇到的一些问题。因此,代码是完全正确的。虽然我不明白为什么我应该把所有的数据放在一个GET行中,而不是干净地隐藏在POST中。我确实理解这两种方法都有效,但你会从哪里看到GET的优势呢?首先,POST还有其他用途。POST可以用于实际编辑数据,而GET只是获取数据,仅此而已。在我看来,更好的做法是使用你正在进行的行动的路线坦克柏油!你是真的!我的代码非常好。我只是错过了将
Content-Type
application/json
作为头参数。非常感谢!:)谢谢Tarlog!你是真的!我的代码非常好。我只是错过了将
Content-Type
application/json
作为头参数。非常感谢!:)谢谢你,彼得!我的错误不是在标题中使用
application/json
设置
Content-Type
。但仍然如此。非常感谢您的代码示例。这将是我要测试的下一个想法。:)谢谢你,彼得!我的错误不是在标题中使用
application/json
设置
Content-Type
。但仍然如此。非常感谢您的代码示例。这将是我要测试的下一个想法。:)