user1056903
user1056903

Reputation: 941

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?

Upvotes: 1

Views: 4046

Answers (1)

bdoughan
bdoughan

Reputation: 149017

By default the root element for your User class will be user, you need to use @XmlRootElement(name="User") to match your XML document.

Upvotes: 3

Related Questions