**For Axon.ivy 6.1 or younger!**
Yes this is quite simple. Just annotate a java service class with JAX-RS annotations to define the interface of your public service.
/**
* Provides the person REST resource
* on the path /ivy/api/myApplicationName/person
*/
@Path("{applicationName}/person")
public class CustomProjectResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Person get() {
Person p = new Person();
p.setFirstname("Renato");
p.setLastname("Stalder");
return p;
}
}
The **ConnectivityDemos** project contains many sample services that demonstrates a REST service definition via JAX-RS annotations. See for instance the `com.axonivy.connectivity.rest.provider.PersonService`.
See also the 3rd Party Integration documentation: http://developer.axonivy.com/doc/latest/DesignerGuideHtml/ivy.integration.html#ivy-integration-rest
----------
With 6.0 or older REST API definition is not supported. But you can anyway provide a simple HTTP service that can be used by REST clients by applying the following solutions:
**PROVIDE DATA (Axon.ivy 6.0 and older)**
If you just want to send business-data (java objects) to a REST client the solution is quit simple. :
1. Create a new RequestStart event as entry URL for the REST request. The start event should not have any input parameters or result configured.
2. Load the data to return (either in plain java or with a process flow)
3. Let the process flow end on a Script activity. This activity is responsible to end the Request and write the serialized JSON directly to the HTTP response. See `ch.ivyteam.q.and.a.RestRequest.sendAsResponse(String)` in the sample project
See [simpleRestProvider_51-v2.iar][2] **customers/get.ivp** for a sample .
**CONSUME AND PROVIDE DATA (Axon.ivy 6.0 and older)**
Solution if you need to get JSON parameters from the client as input, process them and return a result in JSON format.
1. Create a new RequestStart event as entry URL for the REST request. The start event must have a single String parameter which will receive the serialized JSON object(s) from the REST client.
2. De-serialize the input parameter within a Script activity. Use Gson or a similar library for the conversion from JSON to java. Map the de-serialized java object to the process data.
3. Load the data to return (either in plain java or with a process flow)
4. Let the process flow end on a Script activity. This activity is responsible to end the Request and write the serialized JSON directly to the HTTP response. See `ch.ivyteam.q.and.a.RestRequest.sendAsResponse(String)` in the sample project
See [simpleRestProvider_51-v2.iar][2] **customers/filter.ivp** for a sample. For quick review open the java class ch.ivyteam.q.and.a.TestRestMethods, open the context menu of the editor, choose: run as, junit test
**Sample**
- Process
![simple impl screenshot][1]
- Rest Request helper
package ch.ivyteam.q.and.a;
import java.io.IOException;
import java.io.Writer;
import javax.servlet.http.HttpServletRequest;
import ch.ivyteam.ivy.environment.Ivy;
import ch.ivyteam.ivy.request.IHttpRequest;
import ch.ivyteam.ivy.request.IHttpResponse;
import ch.ivyteam.ivy.request.IRequest;
import ch.ivyteam.ivy.request.IResponse;
/**
* Rest request simplifies access to HTTP request/response.
* @author Reguel Wermelinger
*/
public class RestRequest {
/**
* Ends the request and writes given response content manually. Should be called from a Script activity with no outgoing flow.
* @param json
*/
public static void sendAsResponse(String json){
try {
getHttpResponseWriter().append(json);
} catch (IOException e) {
Ivy.log().error("Failed to send json response.", e);
}
}
public static Writer getHttpResponseWriter() {
IResponse response = Ivy.response();
if (!(response instanceof IHttpResponse)) {
throw new RuntimeException(
"Response must be an HTTP response, but was " + response);
}
IHttpResponse httpResponse = (IHttpResponse) response;
try {
return httpResponse.getHttpServletResponse().getWriter();
} catch (IOException ex) {
throw new RuntimeException("Could not get HTTP response writer.", ex);
}
}
public static String getHttpMethod() {
return getHttpServletRequest().getMethod();
}
public static HttpServletRequest getHttpServletRequest() {
IRequest rootRequest = Ivy.request().getRootRequest();
if (!(rootRequest instanceof IHttpRequest)) {
throw new RuntimeException(
"request must be an HTTP request, but was " + rootRequest);
}
IHttpRequest httpRequest = (IHttpRequest) rootRequest;
HttpServletRequest httpServletRequest = httpRequest.getHttpServletRequest();
return httpServletRequest;
}
}
[1]: http://answers.axonivy.com/upfiles/restProcessScreenshot.PNG
[2]: http://developer.axonivy.com/q-and-a-attachments/simpleRestProvider_51-v2.iar/upfiles/simpleRestProvider_51-v2.iar
[3]: http://answers.axonivy.com/upfiles/simpleRestProvider.PNG