Share My Creation Web API Template

Web API
Version: 1.12
Description: A boilerplate for creating CRUD based RESTful Web API.

web-api-help.png


Depends on following libraries: ByteConverter, JavaObject, jServer, Json, jSQL
1630138331344.png


Features:
  1. CRUD based - RESTful Web API (GET, POST, PUT, DELETE)
  2. Front-end (HTML, CSS, JS, Bootstrap)
  3. Support MySQL and SQLite database (Can be modified for MS SQL or SQL Express)
  4. Separate SQL queries file (queries-mysql.ini and queries-sqlite.ini)
  5. Sample database auto-generated for first run (Category and Product tables with dummy data)
  6. Versioning (using ROOT_PATH in config.ini, set as "/" if you don't want versioning)
  7. Auto generated documentation with API test.

Create B4X template from source:
  1. Download the repo from GitHub
  2. Archive the files inside "Web API" directory as "Web API.zip" using WinRAR or 7-Zip
  3. Rename the extension from .zip to .b4xtemplate

How to use:
  1. Copy the "Web API.b4xtemplate" file into B4J Additional Libraries folder.
  2. Open B4J and create a new project with "Web API" template. Give your project any name you like, for e.g. WebAPI
  3. Run the project in Debug or Release mode. You will see something like this in the Logs:
    B4X:
    Web API server (version = 1.12) is running on port 19800
    Open the following URL from your web browser
    http://127.0.0.1:19800/v1/
  4. Copy the URL showed in Logs and open it using your web browser.

5. To connect to MySQL server, go to Objects folder and open "config.ini". Comment the SQLite section and uncomment the MySQL section.​
1630137963547.png
6. Edit the root password at line #42 (second last line). Save the file.​
7. In B4J project, comment the line '#AdditionalJar: sqlite-jdbc-3.36.0.2 and uncomment the line #AdditionalJar: mysql-connector-java-5.1.49-bin.​
Make sure you are using the correct version of connector.​
B4X:
#Region AdditionalJar
' MySQL connector
#AdditionalJar: mysql-connector-java-5.1.49-bin
' SQLite connector
'#AdditionalJar: sqlite-jdbc-3.36.0.2
#End Region
8. Follow step #3 above.​

Note: "Web API Client.b4xtemplate" is also attached to demonstrate how to consume the Web API using B4A, B4i and B4J

About auto generated help file (version 1.10+)

https://www.b4x.com/android/forum/threads/web-api-template.133764/post-851114

Screenshot:
web-api.png


Client App (B4X):
B4A.png
. .
B4i.png
. .
B4J.png


Live demo: https://api.puterise.com:19900/v1/

Video:

Comments: I try to make this template as compact as possible (remove user table and email function) but it is still big. If you don't like some features, just remove them. 😄

Updates:
28/05/2022
- Web API - v1.12 (fixed a bug in HelpHandler.bas when reading commented line)
- Updated to use jServer v4.0
- Web API Client - v1.0.3 (Converted to B4XTemplate)
26/01/2022
- Create Publish.zip using #CustomBuildAction
- Create Publish.zip using Comment Link
19/10/2021 - v1.11
- Better use of Connection Pool for MySQL (do not close pool)
- Change DataUtils module to DataConnector class
- Clean up some redundant codes
8/10/2021
- WebAPI Client - v1.02
- - Some bugs fix and UI improvements
24/9/2021 - v1.10
- Auto generated documentation/help file based on handler classes (Click on the Question mark icon on top right corner to access the help file)
6/9/2021
- WebAPI Client - v1.01
- - Some bugs fix and UI improvements
4/9/2021 - v1.09
- Fixed PutProductByCategoryAndId to update Product as different Category.
- WebAPI Client (B4X) - v1.00
2/9/2021 - v1.08
- Added ConfigureCORS for ajax request.
1/9/2021 - v1.07
- Changes in Utility module (ReturnSuccess and ReturnSuccess2) require Status Code.
- Changes in ProductHandler (PostCategory and PostProductByCategory) return new record as result with Status Code 201.
27/8/2021 - v1.06
- Support for SQLite database
 

Attachments

  • ajax-test.zip
    30.2 KB · Views: 349
  • Web API Client.b4xtemplate
    444.4 KB · Views: 87
  • Web API.b4xtemplate
    462.6 KB · Views: 97
Last edited:

Nick McCulloch

Member
Licensed User
Hi,

Just tried to set this up but I get a bunch of errors in the creation of the sqlite db as follows:

Waiting for debugger to connect...
Program started.
Reading file (E:\Projects\B4J\WAFolder\WebApi\Objects\config.ini)...
Reading file (E:\Projects\B4J\WAFolder\WebApi\Objects\queries-sqlite.ini)...
2022-03-06 15:52:14.422:INFO::main: Logging initialized @625ms to org.eclipse.jetty.util.log.StdErrLog
Checking database...
Database not found!
Creating database...
java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (near ",": syntax error)
at org.sqlite.DB.newSQLException(DB.java:383)
at org.sqlite.DB.newSQLException(DB.java:387)
at org.sqlite.DB.throwex(DB.java:374)
at org.sqlite.NativeDB.prepare(Native Method)
at org.sqlite.DB.prepare(DB.java:123)
at org.sqlite.PrepStmt.<init>(PrepStmt.java:42)
at org.sqlite.Conn.prepareStatement(Conn.java:404)
at org.sqlite.Conn.prepareStatement(Conn.java:399)
at org.sqlite.Conn.prepareStatement(Conn.java:383)
at anywheresoftware.b4j.objects.SQL$2.run(SQL.java:264)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
2022-03-06 15:52:26.385:INFO:eek:ejs.Server:main: jetty-9.4.z-SNAPSHOT; built: 2018-05-03T15:56:21.710Z; git: daa59876e6f384329b122929e70a80934569428c; jvm 11.0.1+13
2022-03-06 15:52:26.408:INFO:eek:ejs.session:main: DefaultSessionIdManager workerName=node0
2022-03-06 15:52:26.408:INFO:eek:ejs.session:main: No SessionScavenger set, using defaults
2022-03-06 15:52:26.409:INFO:eek:ejs.session:main: node0 Scavenging every 660000ms
2022-03-06 15:52:26.413:INFO:eek:ejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@482bce4f{/,file:///E:/Projects/B4J/WAFolder/WebApi/Objects/www/,AVAILABLE}
2022-03-06 15:52:26.416:INFO:eek:ejs.AbstractNCSARequestLog:main: Opened E:\Projects\B4J\WAFolder\WebApi\Objects\logs\b4j-2022_03_06.request.log
2022-03-06 15:52:26.561:INFO:eek:ejs.AbstractConnector:main: Started ServerConnector@32b260fa{HTTP/1.1,[http/1.1]}{0.0.0.0:19800}
2022-03-06 15:52:26.561:INFO:eek:ejs.Server:main: Started @12767ms
Emulated network latency: 100ms
Web API server (version = 1.11) is running on port 19800
Open the following URL from your web browser
Database creation failed!


This is using sqlite-jdbc-3.7.2 , can anyone help?

Nick
 

aeric

Expert
Licensed User
Longtime User
Web API - v1.12 (fixed a bug in HelpHandler.bas when reading commented line)
Web API Client - v1.0.3 (Converted to B4XTemplate)
 
Last edited:

rasaliad

Member
Licensed User
Longtime User
Hi,

I'm try to compile a project with Web API Template and get this error:

B4J Version: 9.30
Parsing code. (0.02s)
Java Version: 11
Building folders structure. (0.01s)
Compiling code. (0.02s)
Compiling layouts code. (0.00s)
Organizing libraries. (0.00s)
Compiling generated Java code. Error
src\b4j\webapi\corsfilter.java:6: error: package jakarta.servlet does not exist
import jakarta.servlet.DispatcherType;
^
1 error

javac 11.0.1


Thanks in advanced


Rafael
 

rasaliad

Member
Licensed User
Longtime User
Thanks rosippc64a

I downloaded the jar from the link you give to me, but the error persist.

I download and put in (I put in the Libraries folder first, but not work):

Jakarta.JPG


where i have to copy the .jar file?

Thanks
 

aeric

Expert
Licensed User
Longtime User
Hi,

I'm try to compile a project with Web API Template and get this error:

B4J Version: 9.30
Parsing code. (0.02s)
Java Version: 11
Building folders structure. (0.01s)
Compiling code. (0.02s)
Compiling layouts code. (0.00s)
Organizing libraries. (0.00s)
Compiling generated Java code. Error
src\b4j\webapi\corsfilter.java:6: error: package jakarta.servlet does not exist
import jakarta.servlet.DispatcherType;
^
1 error

javac 11.0.1


Thanks in advanced


Rafael
Edit: Sorry, I forgot to mention about I updated to new jServer 4. I update the first post.
 
Last edited:

rasaliad

Member
Licensed User
Longtime User
Edit: Sorry, I forgot to mention about I updated to new jServer 4. I update the first post.
thanks aeric

Yes I begin to use Web API Template today. I'm using the last version

webapi112.JPG


And I'm using the jServer V4.0

Jakarta.JPG


The Web API compile Ok if I comment the jakarta import lines and uncomment the javax importe lines in the CorsFilter file.

JakartaCORS.JPG


the error reported using the jakarta import when compiling is:


B4J Version: 9.30
Parsing code. (0.02s)
Java Version: 11
Building folders structure. (0.01s)
Compiling code. (0.02s)
Compiling layouts code. (0.00s)
Organizing libraries. (0.00s)
Compiling generated Java code. Error
src\b4j\webapi\corsfilter.java:6: error: package jakarta.servlet does not exist
import jakarta.servlet.DispatcherType;
^
1 error

javac 11.0.1

What i'm doing wrong?

Thanks again.

Rafael
 

aeric

Expert
Licensed User
Longtime User
thanks aeric

Yes I begin to use Web API Template today. I'm using the last version

View attachment 130542

And I'm using the jServer V4.0

View attachment 130543

The Web API compile Ok if I comment the jakarta import lines and uncomment the javax importe lines in the CorsFilter file.

View attachment 130544

the error reported using the jakarta import when compiling is:


B4J Version: 9.30
Parsing code. (0.02s)
Java Version: 11
Building folders structure. (0.01s)
Compiling code. (0.02s)
Compiling layouts code. (0.00s)
Organizing libraries. (0.00s)
Compiling generated Java code. Error
src\b4j\webapi\corsfilter.java:6: error: package jakarta.servlet does not exist
import jakarta.servlet.DispatcherType;
^
1 error

javac 11.0.1

What i'm doing wrong?

Thanks again.

Rafael
I need to double check. At the meantime, can you try find previous version “web api.b4xtemplate” from GitHub v1.11 and use jServer v3.0 ?
 

rasaliad

Member
Licensed User
Longtime User
I need to double check. At the meantime, can you try find previous version “web api.b4xtemplate” from GitHub v1.11 and use jServer v3.0 ?
thanks aeirc

For now i'm using your web api commenting the import in the CorsFilter file, like this:

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
//import jakarta.servlet.DispatcherType;
//import jakarta.servlet.Filter;

and it is working. Is there any problem doing that?

thanks
 

aeric

Expert
Licensed User
Longtime User
thanks aeirc

For now i'm using your web api commenting the import in the CorsFilter file, like this:

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
//import jakarta.servlet.DispatcherType;
//import jakarta.servlet.Filter;

and it is working. Is there any problem doing that?

thanks
If it is working with this workaround, then you can treat it as a solution. jServer 4.0 is consider a beta so it is optional to upgrade. In case there is an issue, just fall back to the stable version. It is my mistake before go through more testing. You also can exclude the Cors module as it is an additional feature. The Web API can work without this piece.
 

aeric

Expert
Licensed User
Longtime User
Thanks rosippc64a

I downloaded the jar from the link you give to me, but the error persist.

I download and put in (I put in the Libraries folder first, but not work):

View attachment 130532

where i have to copy the .jar file?

Thanks
By the way, I don’t recommend to download the library from different source.
Please download the library provided by Erel in the jServer v4.0 link.
 

rasaliad

Member
Licensed User
Longtime User
By the way, I don’t recommend to download the library from different source.
Please download the library provided by Erel in the jServer v4.0 link.
Hi

Yes i download from Erel post. Thansk for the advice.
 

aeric

Expert
Licensed User
Longtime User
thanks aeric

Yes I begin to use Web API Template today. I'm using the last version

View attachment 130542

And I'm using the jServer V4.0

View attachment 130543

The Web API compile Ok if I comment the jakarta import lines and uncomment the javax importe lines in the CorsFilter file.

View attachment 130544

the error reported using the jakarta import when compiling is:


B4J Version: 9.30
Parsing code. (0.02s)
Java Version: 11
Building folders structure. (0.01s)
Compiling code. (0.02s)
Compiling layouts code. (0.00s)
Organizing libraries. (0.00s)
Compiling generated Java code. Error
src\b4j\webapi\corsfilter.java:6: error: package jakarta.servlet does not exist
import jakarta.servlet.DispatcherType;
^
1 error

javac 11.0.1

What i'm doing wrong?

Thanks again.

Rafael
It seems your jServer folder is different from mine.

1655801801249.png
 

aeric

Expert
Licensed User
Longtime User
thanks aeric

Yes I begin to use Web API Template today. I'm using the last version

View attachment 130542

And I'm using the jServer V4.0

View attachment 130543

The Web API compile Ok if I comment the jakarta import lines and uncomment the javax importe lines in the CorsFilter file.

View attachment 130544

the error reported using the jakarta import when compiling is:


B4J Version: 9.30
Parsing code. (0.02s)
Java Version: 11
Building folders structure. (0.01s)
Compiling code. (0.02s)
Compiling layouts code. (0.00s)
Organizing libraries. (0.00s)
Compiling generated Java code. Error
src\b4j\webapi\corsfilter.java:6: error: package jakarta.servlet does not exist
import jakarta.servlet.DispatcherType;
^
1 error

javac 11.0.1

What i'm doing wrong?

Thanks again.

Rafael
I am unable to reproduce your error. I noticed you are still using B4J version 9.30. Maybe that is the issue. I am using B4J version 9.50 and version 9.80 will be released very soon. You should use more recent version.

I also mentioned that your jServer files also different from mine. Please try to use the new jServer.jar from here or wait for B4J version 9.80.
 
Top