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.
Related
I need to send HTTP requests from my Quarkus application. Following this guide, I have this RestClient:
#Path("/v1")
#RegisterRestClient
public interface CountriesService {
#GET
#Path("/name/{name}")
Set<Country> getByName(#PathParam String name);
}
In the Path annotation, I can configure the path. But the domain/url to call is defined in a configuration file, according to this paragraph.
# Your configuration properties
org.acme.rest.client.CountriesService/mp-rest/url=https://restcountries.eu/rest #
org.acme.rest.client.CountriesService/mp-rest/scope=javax.inject.Singleton #
In my case, I need this URL to be defined programmatically at runtime, as I receive it as a callback URL.
Is there a way to do that?
Quarkus Rest Client, and Quarkus Rest Client Reactive, implement the MicroProfile Rest specification and as such allow creating client stubs with RestClientBuilder programmatically, e.g.:
public class SomeService {
public Response doWorkAgainstApi(URI apiUri, ApiModel apiModel) {
RemoteApi remoteApi = RestClientBuilder.newBuilder()
.baseUri(apiUri)
.build(RemoteApi.class);
return remoteApi.execute(apiModel);
}
}
See https://download.eclipse.org/microprofile/microprofile-rest-client-2.0/microprofile-rest-client-spec-2.0.html#_sample_builder_usage
You cannot achieve this with client created with the #RegisterRestClient annotation
I have set up the following JAXRS service in:
#Path(value="/validationService")
public class ValidationService {
#GET
#Produces(value="text/plain")
#Path(value="{token}")
public String getPropety(#PathParam("token") String token) {
String status = AuthenticationManager.getInstance().getTokenStatus(token);
return status;
}
}
If I hit the url, I receive the proper response on my screen. How can I consume this resouce in the back end? I have set this up using jersey jars and the Cient / CLientBuilder / WebTarget procedure, using TOMCAT, but this does not seem to work with WebsphereApplicationServer 8.5. Any info is appreciated.
answer:
JAX-RS Jersey Client on Websphere 8.5
I was using the incorrect libraries to perform these operations.
I was using: javax.ws.rs to build the client, when I should've been using: org.apache.wink
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
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 can I consume JSON object in my RESTful web server. How should be the client. I'm using Jersey server.
EX.
//BEAN
public class Student{
public String name;
}
//SERVER
#POST #Consumes("application/json")
#Path("/create")
public void create(Student s1) {
System.out.println(s1.name);
}
//CLIENT
ClientConfig config1 = new DefaultClientConfig();
Client client1 = Client.create(config1);
client1.addFilter(new LoggingFilter());
WebResource service1 = client1.resource(getBaseURI());
Student s = new WSTestClient.Student();
s.name="POP";
service1.path("create").type(MediaType.APPLICATION_JSON).post(s);
Its not working...
Most of the REST web services development framework does the marshalling of json to objects. You need to consume a json form param in POST request and assign it to a java bean object. Here is a sample method contract for the same using Jersey framework:
#POST
#Consumes(MediaType.APPLICATION_JSON)
public Response sampleRestMethod(#FormParam ("jsonStringParamName") YourCorrespondingJavaBean yourCorrespondingJavaBeanObj );
Follow this simple but really good tutorial to learn more about writing a RESTful service using jersey and json as the content type:
http://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/
Problem in my client I have to add the following line to make the client post a JSON Object
config1.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE)
Have to add POJO Mapping to the ClientConfig.