How to send response using Restful webservices? - java

I am using Jersey RESTful webservices. i have below web method.
#DELETE
#Path("/{name}_{id}")
#Produces({MediaType.APPLICATION_XML})
public Response deletePerson(#PathParam("name") String name, #PathParam("id") String id) {
personService.deletePerson("id");
return Response.status(200).build();
}
How can i pass parameters to delete using SOAP UI ?
Thanks!

DELETE is the HTTP method you'll have to use, so you nedd to configure it in SOAP UI Rest Resource where you can configure parameters as well

Related

Invoking #POST in jax-rs Endpoint

I have an jax-rs endpoint as below. I need to post a message to a web page through this endpoint. When I execute the endpoint using a client the method with #GET executes. But the method with #POST does not execute. I need to know when will be the #POST method will execute. What should I do to invoke the #POST method.
#GET
#Path("/")
#Produces("text/plain")
public boolean getLoginStatus(#Context HttpServletRequest request) throws URISyntaxException {
return true;
}
#POST
#Path("/")
public boolean helloPost() {
return true;
}
You need to invoke a HTTP POST request from your client - be it a programmatic one (e.g. JAX-RS 2.0 client API), a browser or tools like curl etc. I would strongly suggest using Postman client as a chrome browser extension to execute a POST request and test out your REST service

Consuming a RESTful WebService passing a JSON object as request body

I've defined a RESTful WebService (by using RESTEasy on JBoss AS 7) that consumes a JSON data stream.
#PUT
#Path("/send")
#Consumes(MediaType.APPLICATION_JSON)
public Response consumeJSON(Student student) {
String output = student.toString();
// Do something...
return Response.status(200).entity(output).build();
}
How can I call my WS from another Spring-based webapp, by properly using the RestTemplate, mapping a Java Object to JSON and passing it as request body?
Note: I'm asking about Spring with the aim to investigate the facilities provided by the framework. I well know that it is possible to do that by defining manually the request body.
Cheers, V.
In the client application, you can create an interface with the same signature as the one you expose on the server side, and the same path.
Then, in the spring configuration file, you can use the RESTeasy client API to generate a proxy connecting to the exposed webservice.
In the client application, it would look like this :
SimpleClient.java
#PUT
#Path("/send")
#Consumes(MediaType.APPLICATION_JSON)
public Response consumeJSON(Student student);
Config.java
#Bean
public SimpleClient getSimpleClient(){
Client client = ClientFactory.newClient();
WebTarget target = client.target("http://example.com/base/uri");
ResteasyWebTarget rtarget = (ResteasyWebTarget)target;
SimpleClient simple = rtarget.proxy(SimpleClient.class);
return simple;
}
Then, in the place where you want to invoke this web service, you inject it with Spring and you can call the method. RESTeasy will search for the webservice matching with with your client (according to the path and the request type) and will create a connection.
Launcher.java
#Resource
private SimpleClient simpleClient;
public void sendMessage(Student student) {
simpleClient.consumeJSON(student);
}
Docs on the RESTesay client API : http://docs.jboss.org/resteasy/docs/3.0.7.Final/userguide/html/RESTEasy_Client_Framework.html
Hope this was helpfull.

How to change the response content type for a Dropwizard REST service?

I took a look at the Dropwizard framework and I want to use it to package my existing REST service.
In the tutorial I noticed that the response content type is not set using a ResponseBuilder, can I set the reponse type like I would do for a regular REST service if it were not in a Dropwizard framework ?
The reason I want to set a dynamic response content type is because the webservice does not know the kind of data it is serving.
Thanks
You should be able to just return a Response object and adjust the type. For instance:
#Path("/hello")
#Produces(MediaType.TEXT_PLAIN)
public class Example{
#GET
public Response sayHello() {
return Response.ok("Hello world").type(MediaType.TEXT_HTML).build();
}
}

How to protect restful webservice from unauhtorised access

I have a JAX-RS Restful webservice. This resource/webservice is asset for us. I am exposing this service url to third party client that would call this service resource. I want to protect this service from another authorised client/vendors.
Thus my question is -
How to protect this.
I thought to include an API key and the API key should be matched with a perticuler client API Key. And I also need to match the client url address means from whcih url the request is coming.
How to do this. How to get the URL that is calling the our rest service. I am deploying my rest in Tomcat 7.0.
Thanks
Update --
#Path("report")
#Produces(javax.ws.rs.core.MediaType.APPLICATION_XML)
public class DnaReportResource {
#GET
#Produces(javax.ws.rs.core.MediaType.APPLICATION_XML)
public void getDnaReport(#Context UriInfo uri) {
System.out.println(uri.getRequestUri());
System.out.println(uri.getPath());
System.out.println(uri.getAbsolutePath());
System.out.println(uri.getBaseUri());
//MultivaluedMap<String, String> queryParams = uri.getQueryParameters();
}
}
From your servlet handler you can call
String url = request.getRequestURL().toString();
Getting request URL in a servlet
EDIT
did you try
String url = uri.getRequestUri().toString();
What does it return?
You can make use of Tomcat Realm configuration to achieve authenticate your clients.

Ajax post to RESTful web service

I am having an issue with an Ajax post to a RESTful web service in Java. The project utilizes a single servlet mvc model, with the Ajax post data being sent as JSON to the web service. The specific issue that is occuring is that I a unable to pull the data out of a HttpServletRequest object on the web service side. The POST goes directly to the web service, and I attempted to pull the data out with the following:
#Path(Ajax)
public AjaxResource(){
#Context
HttpServletRequest request;
#POST
#Produces("application/json")
#Consumes("application/json")
public Response postMethod(){
BufferedReader reader = request.getReader();
// additional code
}
}
I receive an IllegalStateException on the getReader() call on the request; from what I understand the input stream/reader can only be called once. I am unsure if this is due to the doPost method in the servlet doing a request.getParameter call as it seems to ago I'd hitting the servlet before this web service. Is there any other way to retrieve this data other than implementing HttpServletRequestWrapper in the servlet?
You should use #Context HttpServletRequest request as an argument of the resource method.
So it should be something like this:
public Response postMethod(#Context HttpServletRequest request){
// rest of the code
}

Categories

Resources