简体   繁体   English

向REST API发送application / xml POST消息不起作用

[英]Sending application/xml POST message to REST API doesn't work

This is the POST method with JAX-RS annotations: 这是使用JAX-RS注释的POST方法:

@POST
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.TEXT_PLAIN)
public Response storeUser(User user) {

   boolean wasStored = JPAUserStore.storeUser(user);

   if (wasStored) {
      return Response.ok("User was stored.").build();
   } else {
      return Response.status(Status.BAD_REQUEST).build();   
   }

}

And this is the class User with JAXB annotations: 这是带有JAXB注释的类User:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public User {

  @XmlElement(name = "Name")
  protected String name;

  @XmlElement(name = "Address")
  protected String address;

  public void setName(String name) {
     this.name = name;
  }

  public String getName() {
     return this.name;
  }

  public void setAddress(String address) {
     this.address = address;
  }

  public String getAddress() {
     return this.address;
  }

}

The REST web service runs on a Jetty. REST Web服务在Jetty上运行。 When I send a request message (using RESTClient Firefox plugin) with content type "application/xml" and this body 当我发送一个请求消息(使用RESTClient Firefox插件),内容类型为“application / xml”和本机构

 <?xml version="1.0" encoding="UTF-8"?>
 <User>
    <Name>Max</Name>
    <Address>Main Street 12</Address>
 </User>

to the appropriate resource a 400 Bad request will be returned. 到适当的资源,将返回400 Bad请求。 According to the log the method JPAUserStore.storeUser(...) was not executed. 根据日志,没有执行JPAUserStore.storeUser(...)方法。

What is the reason, why the method annotated with @POST will be not executed and OK returned? 是什么原因,为什么使用@POST注释的方法将不会被执行并返回OK?

默认情况下, User类的根元素是user ,您需要使用@XmlRootElement(name="User")来匹配XML文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM