Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

REST Service - How to send XML Request from Chrome or Intellij's REST Explorer?

 
Mike London
Ranch Hand
Posts: 1290
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a basic question, perhaps I'm confused, but I have a simple JAX-RS application and we want to send an XML Request, like you would do in Chrome or from any REST explorer.

I have a test "client" program running that works, but it has commands like this:

Response response = client.target("http://localhost:8080/services/customers")
.request().post(Entity.xml(xml));

(using the project's jetty server.)

But, I want to call the service directly from the REST explorer.

So, if you have an XML Request like the one in the JAX-RS 2.0 Book (ch 3), how would you "POST" it to the JAX-RS Service?

Here's the XML Request from the book, from the client code class:

String xml = "<customer>"
+ "<first-name>Bill</first-name>"
+ "<last-name>Burke</last-name>"
+ "<street>256 Clarendon Street</street>"
+ "<city>Boston</city>"
+ "<state>MA</state>"
+ "<zip>02115</zip>"
+ "<country>USA</country>"
+ "</customer>";

So, said another way, I just want to call the REST web service with a REST client and pass the XML Request for a POST.

I took the commented Customer string above and used this as a test to POST as the customer XML:

The REQUEST I USED:
===============

<customer>
<first-name>Bill</first-name>
<last-name>Burke</last-name>
<street>256 Clarendon Street</street>
<city>Boston</city>
<state>MA</state>
<zip>02115</zip>
<country>USA</country>
</customer>

==================

Then, in Intellij's REST Explorer, I have the path to the customer (and the web apps name), like this:

http://localhost:8080/jaxrs-2.0-workbook-ex03_1-2.0/services/customers

(this URL path is for the deployed service in Tomcat.)

I put the Customer XML from above in as the Request and tried to do a POST.

But I get an "Empty Response" return (but not an error).

The application class has this setup:

@ApplicationPath("/services")
public class ShoppingApplication extends Application {
private Set<Object> singletons = new HashSet<Object>();

----------

Here's the actual code in the service class:

@Path("/customers")
public class CustomerResource
.
.
.
@POST
@Consumes("application/xml")
public Response createCustomer(InputStream is)
{
Customer customer = readCustomer(is);
customer.setId(idCounter.incrementAndGet());
customerDB.put(customer.getId(), customer);
System.out.println("Created customer " + customer.getId());
return Response.created(URI.create("/customers/" + customer.getId())).build() ;

}

For simplicity, this example uses a ConncurrentHashMap as a "database".

-----

Do I need to wrap the Request XML with some tag to help things along or am I totally confused?

Thanks very much in advance.

- mike
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!