<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: Can I expose api as REST service?</title><link>https://answers.axonivy.com/questions/1453/can-i-expose-api-as-rest-service</link><description>&lt;p&gt;Is it possible to implement a REST service so that third party system can access ivy core API or any other custom data in JSON format?&lt;/p&gt;</description><atom:link href="https://answers.axonivy.com/questions/1453/can-i-expose-api-as-rest-service" rel="self"></atom:link><language>en</language><lastBuildDate>Thu, 24 Mar 2016 09:59:36 -0400</lastBuildDate><item><title>Comment by Reguel Wermelinger on Reguel Wermelinger's answer</title><link>https://answers.axonivy.com/questions/1453/can-i-expose-api-as-rest-service#1699</link><description>&lt;p&gt;You can test the demo application easily with the REST client in google chrome: Advanced REST client
&lt;img alt="alt text" src="http://answers.axonivy.com/upfiles/restCallAsFormPayload.png"&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Reguel Wermelinger</dc:creator><pubDate>Thu, 24 Mar 2016 09:59:36 -0400</pubDate><guid>https://answers.axonivy.com/questions/1453/can-i-expose-api-as-rest-service#1699</guid></item><item><title>Answer by Reguel Wermelinger</title><link>https://answers.axonivy.com/questions/1453/can-i-expose-api-as-rest-service/1454</link><description>&lt;p&gt;&lt;strong&gt;For Axon.ivy 6.1 or younger!&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Yes this is quite simple. Just annotate a java service class with JAX-RS annotations to define the interface of your public service.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;/**
 * 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;
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;strong&gt;ConnectivityDemos&lt;/strong&gt; project contains many sample services that demonstrates a REST service definition via JAX-RS annotations. See for instance the &lt;code&gt;com.axonivy.connectivity.rest.provider.PersonService&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;See also the 3rd Party Integration documentation: &lt;a href="http://developer.axonivy.com/doc/latest/DesignerGuideHtml/ivy.integration.html#ivy-integration-rest"&gt;http://developer.axonivy.com/doc/latest/DesignerGuideHtml/ivy.integration.html#ivy-integration-rest&lt;/a&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;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:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;PROVIDE DATA (Axon.ivy 6.0 and older)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you just want to send business-data (java objects) to a REST client the solution is quit simple. :&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create a new RequestStart event as entry URL for the REST request. The start event should not have any input parameters or result configured.&lt;/li&gt;
&lt;li&gt;Load the data to return (either in plain java or with a process flow)&lt;/li&gt;
&lt;li&gt;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 &lt;code&gt;ch.ivyteam.q.and.a.RestRequest.sendAsResponse(String)&lt;/code&gt; in the sample project&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;See &lt;a href="/upfiles/simpleRestProvider_51-v2.iar"&gt;simpleRestProvider_51-v2.iar&lt;/a&gt; &lt;strong&gt;customers/get.ivp&lt;/strong&gt; for a sample .&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;CONSUME AND PROVIDE DATA (Axon.ivy 6.0 and older)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Solution if you need to get JSON parameters from the client as input, process them and return a result in JSON format.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;li&gt;Load the data to return (either in plain java or with a process flow)&lt;/li&gt;
&lt;li&gt;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 &lt;code&gt;ch.ivyteam.q.and.a.RestRequest.sendAsResponse(String)&lt;/code&gt; in the sample project&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;See &lt;a href="/upfiles/simpleRestProvider_51-v2.iar"&gt;simpleRestProvider_51-v2.iar&lt;/a&gt; &lt;strong&gt;customers/filter.ivp&lt;/strong&gt; 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&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sample&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Process
&lt;img alt="simple impl screenshot" src="http://answers.axonivy.com/upfiles/restProcessScreenshot.PNG"&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Rest Request helper&lt;/p&gt;
&lt;p&gt;package ch.ivyteam.q.and.a;&lt;/p&gt;
&lt;p&gt;import java.io.IOException;
import java.io.Writer;&lt;/p&gt;
&lt;p&gt;import javax.servlet.http.HttpServletRequest;&lt;/p&gt;
&lt;p&gt;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;&lt;/p&gt;
&lt;p&gt;/*&lt;em&gt;
 * Rest request simplifies access to HTTP request/response.
 * @author Reguel Wermelinger
 &lt;/em&gt;/
public class RestRequest {&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;/**
 * 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;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Reguel Wermelinger</dc:creator><pubDate>Mon, 31 Aug 2015 11:30:43 -0400</pubDate><guid>https://answers.axonivy.com/questions/1453/can-i-expose-api-as-rest-service/1454</guid></item></channel></rss>