简体   繁体   中英

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

This is the POST method with JAX-RS annotations:

@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:

@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. When I send a request message (using RESTClient Firefox plugin) with content type "application/xml" and this body

 <?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. According to the log the method JPAUserStore.storeUser(...) was not executed.

What is the reason, why the method annotated with @POST will be not executed and OK returned?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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