webservice @POST返回415不支持的媒体类型

时间:2014-01-11 22:51:03

标签: java jboss7.x resteasy

我在jboss-as-7上创建了web服务,它可以使用@GET方法,但是当我尝试添加@POST时,我得到的是“415 Unsupported Media Type”。在客户端上调整了大量代码之后服务器端,现在我只是使用REST客户端进行测试。我在这里错过了什么吗?

web服务:

@Stateless
@LocalBean
@Path("/RESTService")
public class ReservationsResource {

    @EJB
    private ReservationsSB reservationsSB;

    @GET
    @Produces("application/xml")
    @Path("/reservation/{id}")
    public Reservations getReservation(@PathParam("id") int id) {
        return reservationsSB.getReservation(id);
    }

    @POST
    @Consumes("application/xml")
    @Path("/reservation/delete")
    public Response deleteReservation(Reservations r){
        edited = null;
        reservationsSB.deleteReservation(r);

        return Response.status(201).entity(r).build();
    }

实体:

@Entity
@XmlRootElement
@Table(name="reservations")
public class Reservations {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    @Column
    private String date;
    private String owner;
    private int active;

    @ManyToOne
    private Tables table;

    public Reservations(String date, String owner, Tables table, int active) {
        super();
        this.date = date;
        this.owner = owner;
        this.table = table;
        this.active = active;
    }
        ...
}

REST客户端请求: 网址:http://localhost:8080/BarBar/RESTService/reservation/delete body(与getReservation()返回的相同):

<reservations>
<active>1</active>
<date>2014-01-14 21:00:00.0</date>
<id>23</id>
<owner>dqf</owner>
<table>
<capacity>6</capacity>
<id>30</id>
<name>table 4</name>
</table>
</reservations>

1 个答案:

答案 0 :(得分:2)

确保将Content-TypeAccept标头设置为 application / xml 。此外,这不一定是关键,但请考虑将您的方法设置为@DELETE以使用适当的REST语义。