【问题标题】:webservice @POST returns 415 Unsupported Media Typewebservice @POST 返回 415 Unsupported Media Type
【发布时间】:2014-01-11 22:51:02
【问题描述】:

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

网络服务:

@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 正文(与 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>

【问题讨论】:

    标签: java jboss7.x resteasy


    【解决方案1】:

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

    【讨论】:

    • 非常感谢!接受标头丢失
    • 很高兴为您提供帮助。祝你的项目好运!
    猜你喜欢
    • 2021-07-16
    • 1970-01-01
    • 2022-10-04
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    相关资源
    最近更新 更多