Help with setting up a Java REST API

Foxhound5366

Executive Member
Joined
Oct 23, 2014
Messages
6,522
#1
I need some help from anybody with experience hosting and setting up Java REST APIs.

I've developed a website that relies on sending queries to an online Java REST API, by constructing URLs like this: www.[host].com/api + [request]. The API returns JSON responses that I decode with some Javascript.

I now want to host that Java REST API myself, but I can't do it on my standard LAMP website hosting ... so I need a third-party solution.

Because I've got no clue how to go about the above (I've had recommendations of Amazon's Lambda functions ... but I can't understand a word of it), is there any service provider in South Africa who could set up that API for me and just restrict it to only respond to calls from my website? Ideally I'd only have to pay for the setup fee and ongoing monthly API hosting.

Thanks!
 

Hamster

Resident Rodent
Joined
Aug 22, 2006
Messages
30,138
#3
You can always host it on a VPS (any SA provider like HostKing, Afrihost etc. or Digital Ocean, SkySilk etc. if you want cheaper international hosting).

All you really need is Java, Tomcat/WildFly/Jetty and open port 80/443 if you don't come host on the same server.

You can restrict access to specific domains/ip addresses either in code (request.getServerName()), .htaccess or something similar.
 

Foxhound5366

Executive Member
Joined
Oct 23, 2014
Messages
6,522
#4
Yeah Hamster ... I just don't know how to do that :p

Right now I'm focusing on an alternative PHP-based version of the API I want, so I'm teaching myself some basic AJAX to get my JavaScript talking to the PHP. Let's hope it works :)
 

Hamish McPanji

Honorary Master
Joined
Oct 29, 2009
Messages
36,612
#7
I had actually type out "why Java?" as well. But thought maybe I didn't grasp exactly what you were trying to do.
 

Hamster

Resident Rodent
Joined
Aug 22, 2006
Messages
30,138
#8
Same here but I read he already developed it...

Yeah Hamster ... I just don't know how to do that:p

Right now I'm focusing on an alternative PHP-based version of the API I want, so I'm teaching myself some basic AJAX to get my JavaScript talking to the PHP. Let's hope it works :)
It's not difficult to be honest and way less effort than rewriting the API:
https://linuxize.com/post/how-to-install-tomcat-9-on-centos-7/

Then open the firewall ports.

Then in Java (probably not the best way) you add
Code:
request.getServerName().equalsIgnoreCase("foxhound.com")
checks to your REST endpoints and return with 403 statuses if it doesn't match.

EDIT: You want to look at CORS for limiting hosts etc. if you are up to it.
 
Last edited:

gkm

Expert Member
Joined
May 10, 2005
Messages
1,483
#9
If you have something already written, the AWS ElasticBeanstalk mentioned above is a good option.

Otherwise, in terms of using AWS API Gateway and Lambda, I agree it is very confusing in the beginning, because there is a bunch of new terminology and concepts to deal with. I was also pretty confused when I wrote my first Lambda function. However, once you have it working, you are going to be very happy. The way you simply do not have to deal with any of the cruft around managing a server and environment and stack and have it automatically scale up and down to your load, while you only pay for what you use, possibly even staying in the free tier initially when your volume is low, is just so nice.

Search online for a couple of tutorials (e.g. the ones leading up to https://www.baeldung.com/aws-lambda-api-gateway ) and follow them and soon you will have your head wrapped around what is needed. Then you can slot your own functionality into the very cookie cutter patterns, using whatever development language you like.
 

Foxhound5366

Executive Member
Joined
Oct 23, 2014
Messages
6,522
#10
Thanks for the guidance guys :) Firstly the API I want to use isn't mine, it's open-source and all the code is ready to set up ... if you know how to. My website just calls to that API.

Luckily somebody pointed me in the direction of AJAX, which seems to be the perfect bridge between the PHP version of the API and the JavaScript I'm already using on my site to call for the API and decode its responses. I'm trying to give myself an AJAX crash course now ... so far I've managed to call to a test PHP script on my server and get a response back, but the response is 0 and not the echo of the argument I sent it ... gonna have to figure out what I'm doing wrong there :D
 

zippy

Executive Member
Joined
May 31, 2005
Messages
9,667
#11
I need some help from anybody with experience hosting and setting up Java REST APIs.

I've developed a website that relies on sending queries to an online Java REST API, by constructing URLs like this: www.[host].com/api + [request]. The API returns JSON responses that I decode with some Javascript.

I now want to host that Java REST API myself, but I can't do it on my standard LAMP website hosting ... so I need a third-party solution.

Because I've got no clue how to go about the above (I've had recommendations of Amazon's Lambda functions ... but I can't understand a word of it), is there any service provider in South Africa who could set up that API for me and just restrict it to only respond to calls from my website? Ideally I'd only have to pay for the setup fee and ongoing monthly API hosting.

Thanks!
Check out Spring Boot. Loads of tutorials on the net, YouTube etc
 

Foxhound5366

Executive Member
Joined
Oct 23, 2014
Messages
6,522
#12
Thanks again for all the help guys, I managed to get the AJAX and PHP working, so no immediate need for the online API anymore. Now I've moved onto beta testing (my other thread here).
 
Top