Reputation: 9757
I recently asked this question: Question I asked recently
I like the restful way that link is represented BTW. The question was essentially how do I get complex parameters to my REST service? What would the code and parameters of that code look like? Well, the more I thought about it, the more it reminded me of a simple a web form submission. Keep in mind that the clients of this service are going to be native applications. Why can't the client applications assemble the variables in questions into a post request Key-value object (including a byte array-file), bundle that and send it to my service where the appropriate action/response will occur? Pretty sure that Java (RESTEasy is the framework I am using) can handle the request gracefully. Am I crazy or has this already been worked out?
As an example of how this would look does anybody have a sample HTML string that would represent a simple post of a couple of variables, like this?
{
"restriction-type": "boolean-search-restriction",
"boolean-logic": "and",
"restrictions": [
{
"restriction-type": "property-search-restriction",
"property": {
"name": "name",
"type": "STRING"
},
"match-mode": "EXACTLY_MATCHES",
"value": "admin"
},
{
"restriction-type": "property-search-restriction",
"property": {
"name": "email",
"type": "STRING"
},
"match-mode": "EXACTLY_MATCHES",
"value": "[email protected]"
}
]
}
But with html headers and all??? I got that example from here btw: example JSON post
Upvotes: 0
Views: 392
Reputation: 1685
The RestEasy
framework already provides a JAX-RS client
implementation unless if you want to start from scratch using HttpURLConnection
or even HttpClient
from Apache HttpComponents
.
Anyhow as long as the question is related to RESTEasy I will provide an example on the latter framework.
If the post looks like this :
@Path("/client")
public class ClientResource {
@POST
@Consumes("application/json")
@Produces("application/json")
public Response addClient(Client aClient) {
String addMessage=clientService.save(aClient);
return Response.status(201).entity(addMessage).build();
}
...
}
A basicRestEasy Client
call would look like this :
public void testClientPost() {
try {
ClientRequest request = new ClientRequest(
"http://localhost:8080/RestService/client");
request.accept("application/json");
Client client=new Client(5,"name","login","password");
//convert your object to json with Google gson
//https://github.com/google/gson
String input = gson.toJson(client);
request.body("application/json", input);
ClientResponse<String> response = request.post(String.class);
if (response.getStatus() != 201) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
//this is used to read the response.
BufferedReader br = new BufferedReader(new InputStreamReader(
new ByteArrayInputStream(response.getEntity().getBytes())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 1