create REST API in java and use in Bonita

1
0
-1

Ok Good evening ,

I followed this tutorial today https://documentation.bonitasoft.com/bonita/7.5/rest-api-extensions
and i must say i enjoyed every bit of it, shows I can create my own REST api and use in bonita. Now there is something i want to be sure of, can i code the REST API in java as well and ask it to show its result in json? then register in bonita? i just need to be clear of certain things thats all.

1 answer

1
0
-1

Bonita REST API extensions are exactly that: a piece of code that can access Bonita Engine API and potentially external services and returns a result in JSON.

Language to create a REST API extension is Groovy. But if you know Java you should have no problem writing Groovy. You can write Groovy using the Java syntax. You find information about the difference between Groovy and Java in Groovy documentation.

Also note that you refer to the documentation for Bonita version 7.5. I always recommend to use the latest version available (today 7.8): https://documentation.bonitasoft.com/bonita/7.8/rest-api-extensions

Comments

Submitted by raysoniali on Tue, 05/14/2019 - 15:02

i was trying to implement the api as you told me the last time , hence i decided to learn Groovy and since i knew java , picking up was not much of an issue ..
Now i want to show u something, i am trying to implement something like this

https://localhost:3000/api/user/2 and it should display the api on the json body

now my groovy rest api looks like this (Had to add jar files for mysql, bonita web extensions, slf4j-api) now my code looks somewhat like this :

package com.peopleInformation


// here i am trying to imitate something of this kind, no counters , nothing here is what the API should actually look like 
//https://localhost/userinformation.php?userId=150


import groovy.sql.Sql

import javax.servlet.http.HttpServletRequest

import org.bonitasoft.web.extension.rest.RestAPIContext
import org.bonitasoft.web.extension.rest.RestApiController
import org.bonitasoft.web.extension.rest.RestApiResponse
import org.bonitasoft.web.extension.rest.RestApiResponseBuilder
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import groovy.json.JsonBuilder

class Index implements RestApiController{
    private static final Logger LOGGER = LoggerFactory.getLogger(Index.class)


    @Override
    public RestApiResponse doHandle(HttpServletRequest request, RestApiResponseBuilder responseBuilder, RestAPIContext context) {

        // Retrieve userId parameter
        def userId = request.getParameter "userId"
        if (userId == null) {
            return buildResponse(responseBuilder, HttpServletResponse.SC_BAD_REQUEST,"""{"error" : "the parameter userId is missing"}""")
        }

        userId = userId as int


        // Initialize the list to store users information

        def usersInformation = []

      // get user information from database
        def connection = Sql.newInstance('jdbc:mysql://localhost:3306/bonitasoft_json','root', '', 'com.mysql.jdbc.Driver')

        def resultSet = connection.rows('select * from people_data where userId = :userId',[userId:it.userId])

        def peopleInfo = [fullname : resultSet.fullname,position : resultSet.position,supervisor : resultSet.supervisor,supervisoremail :               resultSet.supervisoremail,perdiem:resultSet.perdiem]

            usersInformation << peopleInfo

        def result = [userId: userId, peopleInfo: usersInformation]
        return buildPagedResponse(responseBuilder, new JsonBuilder(result).toString())
    }


    RestApiResponse buildPagedResponse(RestApiResponseBuilder responseBuilder, Serializable body) {
        return responseBuilder.with {
            withResponse(body)
            build()
        }
    }
}

The trouble i have now is how i can create the pages.properties file and use the encoding. I am on a windows Machine and notepad++ and notepad wont allow me to do that ISO 8859-1 encoding.

just need some advise here and as regards to my code, am I on track?

Submitted by raysoniali on Tue, 05/14/2019 - 15:47

Ok for the Record, i have been able to save the encoded file in ISO 8859-1 encoding i just used notepad++ and it appears to be fine now
now here is what i want to do, i am going to test and see what comes up with the REST API i created.
many thanks again

Submitted by antoine.mottier on Tue, 05/14/2019 - 15:58

Yes your are on good track. For information a REST API extension that use a data source (for performance consideration) to connect to an external (not Bonita Engine nor Bonita Business Data Model - BDM) already exists. I'll try to review and update the code of this extension soon.

Edit: I just review the code of the extension I mentioned in my comment and it runs without major change with Bonita 7.8.4.

Notifications