Create a simple API in Java with tomcat server - java

I'm in a tricky situation, I'm mainly a frontend dev working in react but I got into a project where i need to create an API with some endpoints in various languages. I have done Python and dot Net already. Now it's time for Java. I'm totally new to all the backend languages except nodejs so sorry if I'm incorrect on some things.
I've set up a Java project with a Tomcat web server. I can run it locally on port 8080.
I want to create a simple API with 3 endpoints that only returns some text.
What is the simplest way of doing this? Thank you!

You can use Spark (https://sparkjava.com/) for that purposes. Its API is similar to Node JS ones. And you can deploy it to the Tomcat.
BTW, Tomcat is the envirenment for running Java applications. It is like Node JS.

Related

Can Electron apps be integrated with java code?

As node.js still lacks important functionality which exists in Java, I would like to use Java instead of node.js, and create the client using a web language (html, js, css..).
Electron is cross platform and so does java so it seems fit to have a solution getting the best of both worlds.
Does someone know of a way to integrate electron with java or have a different solution to the problem?
I made something similar, Java back-end with Electron GUI.
You can do it in more ways, it depends on what you need.
You can create a jar file and then execute it like terminal:
https://nodejs.org/api/child_process.html
Or you can open a socket communication and talk on a Port. (A lot of documentation:
Java (web)socket - Node.js client.io)
In this second way, you can do everything you want, but you have to create your communication protocol.
Your path is not foolish, I am very satisfied of the communication and usage in my work with Java + Electron .
I've created a small PoC where Java process is integrated with Electron front-end: https://github.com/jreznot/electron-java-app There you will find a simple TODO List application built with Vaadin/Jetty and Electron.
Personally i made my back-end java communicate with the front-end by creating a file with te data then sending it to the main.js to be processed.
DBus and winDbus seems to be an option here.
It creates nice abstraction and separation between "frontend" and backend
https://sourceforge.net/projects/windbus/
I'm going to test it on my own soon

Setting up server for android app

I have developed an android application and i need to set a server for sending and receiving data from clients. The server must also be able to run python script for data mining.I want to code the server in java(for analysing the data obtained from mining and handling the client requests).
what will be the way to set the server for my purpose. I have read that servers can be set up in eclipse or installing apache in linux in a virtual machine. will anyone of them fit my need or is there any other better alternative.
Well, it depends on your needs. If you want to run Python scripts from Java, I don't really see why you would code your server-side logic in Java and then use Python data-mining scripts (you might have some problems with execution of Python scripts through Java and server setup). I know that Python is much better in data-mining, so stick with it. Consider Python for server-side logic. In case you want to run data-mining script manually that doesn't really mater, so you can separate the languages.
In case you want to separate it into two languages, then you must install an application server for Java. There are plenty of options for that, even open-sourced (Wildfly, Glassfish, ...):
https://blog.idrsolutions.com/2015/04/top-10-open-source-java-and-javaee-application-servers/
You can set most of them in Eclipse for development, for production releases they can be installed either on Unix or Windows. The Apache option you mentioned is probably Apache Tomcat application server. You can read more about Java application server set-up in Eclipse:
http://www.eclipse.org/webtools/community/education/web/t320/Configuring_an_Application_Server_in_Eclipse.pdf
Depending upon the requirement, you can setup the server in many ways in any operating system like linux (fedora, ubuntu), windows, mac osx etc. There are many java web frameworks to follow to make your own server and provide API for mobile.
These are some java web frameworks:
playframework
Spring MVC
Grails
You can setup above and other frameworks and can setup in any enviorment. For initial, you can setup in your local system, then later you can upload into your remote server.
For setup, please see the respective framework's documentation.
It would be wise to choose json for communication between mobile and server. You can consume the API in android using default HttpURLConnection or you can follow other opensource http connection library like retrofit, asynchttp etc.

serving static content and java web apps from one server

I'm trying to build my own site where initially I will only host my profile and resume but will also build and publish some simple web applications that I can use as coding samples for jobs. The idea is people can see a demo of the apps and then see the code on github.
Can I do all this using only tomcat or should I use another server for this purpose?
I have heard that tomcat should only be used for java web applications but I've tried it locally and it seems to work...
You can totally do it all with tomcat.
Arguably, tomcat is not as efficient as apache or ngnix for serving static assets, 10% less so, and does not scale as well. There's a good article on the subject here:
http://tomcat.apache.org/tomcat-3.2-doc/tomcat-apache-howto.html
This is not not going to be an issue for you with a small site. Go with what gets you up and running the fastest.
If you want, later you can show off your technical prowess by running tomcat behind a web server. You can tell folks at interviews the story.

Simple Java web framework [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Can anyone recommend a simple Java web-app framework?
I want to know about web development with Java. It is very easy to work with PHP and Apache, but I need to use Java.
I have searched for Java web frameworks, but many of them are complex and difficult to configure. I need something as simple as PHP with Apache.
Tomcat and Jetty do not use the port 80, I have to keep a process to redirect connections to another port, and a transfer from a port to another port is inefficient. However, Apache uses port 80 naturally.
There are frameworks that use a complex directory structure with many configuration folders and files. Some of them use the concept of application for each folder. With Apache, it is very simple, because each page is a file in "/var/www/".
I do not know anything about servlet, JSP, JSF or Java Enterprise Edition. I prefer something that mix static HTML and dynamically-generated HTML in the same file, but if I have to call a Java function many times to output HTML string, it's ok.
I want to know a simple Java Web Framework. Something to listen the port 80, interpret the HTTP protocol and show the HTML that is generated by the Java code. The OS is Linux (Ubuntu) and I do not use IDE.
Thanks
You can run Tomcat/Jetty on port 80. Just edit the server.xml (for Tomcat). On Unix/Linux port numbers < 1024 are for privileged users so you would need to run tomcat as root.
Also, running Java web apps is a little bit different than using a LAMP (or a similar) stack. You do still need to understand the concept of a Web/Servlet container
http://en.wikipedia.org/wiki/Servlet_container
Familiarize yourself with the a typical structure for a WAR (Web Archive) file. This is no different than learning about how Apache works and familiarizing oneself with basic Apache configuration with working with a LAMP stack.
This is the bare minimum one should do when venturing into Java Web development.
Take a look at web4j http://www.web4j.com/ I have never used it but it looks simplistic enough for your needs and should get you started
Check out Play: http://www.playframework.org/
I've heard good things about it.
Based on your requirement, I think JSP is all you need. It should be relatively easy to learn and lets you mix dynamic content with static HTML, can be directly accesible just like PHP scripts(If placed in correct folder), and also lets you call Java functions.
Although it requires a bit of expertise, but you can also take a look at mod_jk here: http://diegobenna.blogspot.com/2011/01/connect-tomcat-7-with-apache2-modjk-and.html
It lets you run tomcat alongside Apache, so you have both Apache and Tomcat running side by side.
You will probably want to use Jetty to deploy your servlet. Since you don't use an IDE, if you use maven it's magical to be able to run mvn jetty:run and test your application (on any port). I also recommend maven as an easy way to generate WAR files. Jersey is a light-weight REST framework.
If you are not constrained to Java but rather are constrained to the JVM, I recommend looking at Scalatra for Scala.
Tomcat and Jetty may not use port 80 by default, but they are certainly capable of using port 80.

Creating Apps In Google Web Toolkit, Hosting Options?

So i have been looking into GWT and it does look pretty interesting as i do have experience in programming in java and being able to create webapps does look very interesting to me.
I have looked at some of the basic tutorials and have got GWT working in my eclipse environment. Now, If i was to begin creating webapps with it how would i go about uploading and hosting my app. I have only used PHP and MySQL for web development so i would always just use Apache, I am not to sure about GWT hosting. Does it require the app to be hosted on googles App Engine platform or can I setup a server like to usually do when using Apache?
Thanks.
Using GWT does not require you to host your app on App Engine. You can host a GWT app on any host.
If you want your server to be written in Java, to take advantage of some of the more useful features of GWT, you will obviously need to find a host that allows you to run Java. But you can write a GWT app that communicates with a PHP server, or a Python server, or anything else.
If you're familiar with PHP + MySQL it's not a bad idea to stick to that and just add your compiled GWT output as another component that you upload. Switching to App Engine requires a fundamental change in the way you write and design your application which means you'll be learning new front-end and back-end technologies at the same time. Just as an example, a simple full-text search on one field is pretty trivial for a small php + mysql app but because App Engine forces you to write extremely scalable code it's not trivial on GAE.

Categories

Resources