REST API general questions - java

I'm new to REST API's in general and I have the following questions:
I want to make a website, and an App that share the same users, and data from database in general. For the website I plan to make it with spring for the backend, and for the app I plan to make it in kotlin, as well as an IOS version, to which I havent decided what language I will use. I also want an API the public can use. Is this when I would make a rest API, for the data, and the public API?
The website and app would both contain login, registration, and more
How do I make the website/app retrieve data from the REST API?
Also, what do I make the API in? Is this something I could use spring for?
Sorry if this is a really stupid question.

I am not sure why you want to use kotlin. You can use Angular for UI and Spring Boot for Rest API. I have some POC for user registration. Please have a look.
https://github.com/numery009/springmvc-security-aop-hibernate-user-registration
https://github.com/numery009/numery009.github.io
For UI with Angular -- https://numery009.github.io/

Unlike your question supposes, which is declared as "general", you are mentioning Spring MVC as backend for REST-Endpoint API as well as Angular as independent frontend solution, altogether handling user, groups and their special rights on objects.
There are some dozen if not hundreds of possibilities to build an API, but I suppose you look at jhipster.tech, which is a code generator serving you the basic fundament for Spring backend and Angular frontend (you can choose other frontends, but I recommend to use Angular for generation as it is similar to Java in Spring framework).
As you can generate from easy first monolithic constructs for testing and learning all the modules and technics as well as high-level company-driving microservices for ERP including process-management and accounting, API for field workers, partner companies and clients, one or more Webstores, connecting mobile devices and everything else "in minutes", it is not only a splendid tool, that shows you best practice in development including good documentation, but it is also used as generator for professionals to start a project.
With jhipster and its community you'll learn from pro's and get some steps further - at least for me the work of those guys was and is an eye-opener - after years of reading books about programming and turning left and right, often enough to nowhere.
Of course, it is opinionated like Spring and Angular themselves. But the technics which are used are reliable, well combinated and do build the daily software-stack in many companies since and for years.
You'll code in Java or Kotlin in Spring and ECMAScript (Typescript as Javascript Transpiler) in Angular framework. The structure and ideas behind these programming languages are comparable - "Typescript is easy to understand for Java-guys". And you'll stay independent from support companies, platforms or library changes. Besides you'll learn how to update your own software, which is often forgotten in business software, too.

Related

Is it possible to use more than one framework at the backend(Spring boot + Django)?

tl;dr: Is Spring + Django back-end possible?
When I was new to industry and was still working my way around the office, I got interested in Django and created a very small, basic-level application using the framework. When I got to meet my team after a few weeks, they said to go for Spring framework. After spending half a year on the framework and the main proj, I finally started to get time to start working off-hours. But, I don't want to lose both the skills - My teammate(when we were still in office ;) ) once told me that they worked on a project that started with python code, and then later added features using Java. And I am unable to find any helpful google searches(mostly showing Spring vs Django).
How should I go about it? Is it too much to ask for? Is it worthwhile? Will I learn some new concepts of application architecture a noob like me would have missed. Please provide me with some insight.
Are there resources(docs) I can go through?
P.S. I'm not a diehard fan of either of the frameworks right now, just another coder testing waters.
You can't write java in python.
You can extend Python with C/C++ which is quite common: Extending Python with C or C++
And about the part that they told that they added features with java:
It's common to create different parts of a project using different languages and tools. Microservice architecture is a common architecture for these kinds of use cases. You basically code different parts of the project in a language you want and then you connect all the parts using different methods like REST APIs, gRPC and etc.
Imagine you are creating a website like youtube that lets others upload videos. There is a form that users upload their files and you store them in your storage and then you have to encode the video file for different qualities. You can code the form handler using Python and Django to store the files in your storage. Then you can code another service using java that handles the encoding part which is a heavy process. When an upload is completed, you send the file or file path to your java service using an internal REST API and tell the service to start encoding the video and notify the Django service and then the Django service will publish the video on the feed that can itself be written in another language.
I would say go for 1 framework and stick with it. For example Django if you want to code in python, and spring if you want to code in java. Learning both frameworks however brings a lot of value, because you can compare their benefits (eg. spring forces you to write clean code, django has build-in and simpler database management)
I like Django's build-in tooling a lot, you only need to know python for it to work. Spring requires a bit more knowledge of eg. hibernate for database management. However I predict Django will outgrow spring at some point, because of cloud valuing fast iteration over code and quick startup time (auto-scaling apps) over large overhead apps and long boot times. Hoever, if you like java, I can recommend JHipster for java/spring webapp development to get up to speed very fast and learning the ways of REST CRUD api fast.
To combine 2 programs: write your main logic in one app, and write a small service in the second language, making sure its independent of the first app (no back and forth communication and complicated logic, but simple independent request/response, as if the main app was never there). Add a REST api to the second app and use eg. http requests to communicate.
What's possible in terms of combining languages:
connect different applications with each other: by letting them communicate through their APIs. For example a python api developed with flask or django can send requests to a java api developed with spring, as long as they have a way to communicate (eg over http, or via some queue like rabbitmq)
connect a webapp to 2 different backends: by using a shared authentication system: For example a keycloak authentication server to handle tokens, that your backend applications know about.
What's not possible (and also not preferable):
combining java with python code in the same program: there are some hacky ways to get it to work, but its asking for trouble and not readable.

GWT and Spring integration

I'm currently working on a specification for a web application which consist of a administration interface, and a set of web-services which will be consumed by parter websites.
I been playing around with GWT and it seems like an amazing fit for the administration interface, but at the same time, I would love to use the Spring framework to produce the REST based web-service API exposed by the application.
My plan is to create two separate web applications, one for the admin and one for the web-services. One set up for spring and the other with the GWT app. Obviously, I wish to share the domain model between the two applications, I'm guessing this should be fairly trivial to accomplish? I'm thinking just to keep it simple, implement the full domain model in the GWT client project, and simply setting the sourcepath of the web-service to include the domain model from the relevant folders in the GWT project.
Now, to my question. I'm sure somebody out there have done something similar to this, are there any potential pitfalls I should be aware of before starting out?
I got a fair amount of time set aside for this project, but I never actually used GWT before for anything "serious" I just played around with it out of personal interest and I'm fairly experienced in both Swing and Android UI development, so I'm confident I can mange to use it for the (very simple) UI. The "unknown" in this equation is sharing of a domain model. I cant imagine this being a problem, since the domain model will be just POJO's (using JDBC, but again, I cant really imagine this being a problem).
Also, are there better ways of integrating GWT and Spring than simply doing to separate web applications and sharing their domain model?
Why do you want to create two separate applications?
Create one, with a clearly separated application services tier. Expose this services via standard Spring annotations as webservices, and also expose access to this services via GWT requestfactory (unfortunately, as far as I know, currently requestfactory does not play nice with REST requests, mainly because their way of serialization). This approach results in only one entrance (facade) to your application and allows to use this facade in two different ways.
I've worked on several GWT+Spring projects - for standard cases development goes very fast, frameworks integrates very well. However, if you want to do some customization, e.g. introduce security based on spring security - you will need to do some hacks, such as overriding transport classes. Of course it takes some time, but results are very nice.
I would definitely recommend to take a look at Spring ROO sample applications (as far as I know, roo is partially developed by google guys).

Is making simple website in "java spring hibernate" is overkilling

Initially i was php programmer and was using joomla to build websites .
I could easily build good websites in joomla in few days.
I was learning java spring hibernate for last three months and now i have build simple user registration logins and members area using spring MVC , hibernate annotation etc.
Now i want to ask that whether from now onwards should i use spring MVC to build site just because i have learned it or its good for me in future. Because i still feel that the same site can be build in few days in php
Is it the case that in future i can also come on same level as php that i can build simple site in java in few days as well or no matter what experience i have java still is going to take longer than php always
Can any java CMS make my life easier because i want to say good bye to php.
i mean if i want to make just 5 page portfolio website can build in java cms in 1-2 days
If by "simple website" you mean a normal page with categories, articles and some navigation (i.e. not a web-application), then yes: Building that from the grounds up with Java + Spring + Hibernate is probably overkill (i.e. those are low-level tools, you'd have to build a lot yourself). I'd stick with the tools built for this specific task, namely some kind of CMS system.
If you want to bring "some Java" into your day-to-day job, then you might want to try looking into a Java CMS system.
You should, of course, use the appropriate tools for the job. Spring MVC is not appropriate if you are re-implementing functionality that has already been robustly provided by an existing tool (eg. Joomla etc.).
Given that much of the 'interactive' part of many websites can now be hosted elsewhere (eg. Disqus for comments and so on), you may not need a dynamic website at all and could host the site on one of the many cloud services for static content. This has the advantage that your performance is elastic, and 'cost per user' can be vanishingly low.

Create a webservice that keeps data secure?

I want to create a webservice that allows users to enter the longitude and latitude for a particular building. Buildings will belong to a broader category called a Region. If a specific building does not exist in a region the user could add the longitude and latitude data through the webservice. Similarly if a user finds data to be inaccurate, they could update it.
This webservice would not need to have a web front-end only a RESTful API that would only be accessible to my application(s). I want the webservice to be able to accept new and updated data (as described above) and also print out relevant data (in xml format) based on the appropriate API calls.
I need to ensure that this data would be secure - i.e. nobody would be able to gain access to the data i have accumulated.
What is the best way to do this? I have familiar with client side Java and not much server side technology (not familiar with server-side Java, rails, django, etc...). What language/frameworks should i use that would accomplish my needs with the shallowest learning curve?
You can just use basic authentication (username/pw) with ssl enabled. That way you are encrypting the connection and providing a broadly supported means of authentication. I believe apache Axis will take care of most of it for you.
I've used the Restlet framework to deploy web services that are password protected. It supports basic authentification and several others out of the box. You can also set up your services behind an https "server connector".
Another approach is to run your application in a Java EE application server which supports JSR 196 (eg, Glassfish or JBoss). You would then use the server's facilities to establish the authentication.
Here is the Glassfish security page.
If you have never programmed server-side code you will be facing a pretty steep learning curve, I'm afraid. If you are comfortable with Java then the Restlet framework mentioned by another commenter is a good choice. It is easy to use, includes both client and server tools, and has pretty decent documentation. The video screencasts are very good.
Another option is Ruby on Rails. I am currently implementing something very similar to what you are planning and Rails has worked extremely well. Rails has built-in support for XML output through both the ActiveRecord class and XML Builder templates. I used Atom Authentication (http://www.xml.com/pub/a/2003/12/17/dive.html) between the client and server and it is working beautifully. There is a learning curve for both Ruby and Rails but the power of the framework makes it worth it. I am partial to the Ruby and Rails books at The Pragmatic Programmer but O'Reilly has some good ones, too.

Middleware for MongoDB or CouchDB with jQuery Ajax/JSON frontend

I've been using the following web development stack for a few years:
java/spring/hibernate/mysql/jetty/wicket/jquery
For certain requirements, I'm considering switching to a NoSQL datastore with an AJAX frontend. I would probably build the frontend with jQuery and communicate with the web application middleware using JSON. I'm leaning toward MongoDB because of more dynamic query capabilities, but am still considering CouchDB.
I'm not sure what to use in the middle. Probably something RESTful? My preference is to stick with Java (or maybe Scala or Groovy) since I'm using tools like Drools for rules and Shiro for security. But then again, I want to pick something that is quick an easy to work with, so I'm open to other solutions.
If you are building ajax/json/nosql solutions, I'd like to hear details about what tools you are using and any pros/cons you've found to using them.
Pick whichever middleware you are most comfortable with.
CouchApp is very experimental at the moment. The main issue is being able to add security to your app without having a standard HTTP pop-up box. This is obviously a big issue for standard web apps.
Try and avoid parsing each DB request in the middleware and rebuilding the query for couchdb. You can make your middleware act like a proxy so most requests are forwarded on without modification. You can also add a security layer in the middlelayer on top of all requests that need authentication.
Pick a middleware/framework with good URL routing capabilities. For example you could route all requests that go to mydomain.com/db/ to couchdb.
If you go with CouchDB, you can use CouchApp which is a set of scripts for deploying an application directly to a CouchDB database. In essence, you skip the middleware and use CouchDB's views, lists, and show functions along with clientside JavaScript to implement the whole app. If your app works in this architecture, it's surprisingly refreshing, simple and cool.
Also if you like the idea of JSON/REST and sticking to JavaScript client to server, the newer generation of Persevere's core, Pintura is pure JS JSON/REST framework that is designed specifically to work well with NoSQL DBs.
I've been tinkering with a few. Ultimately, I'd like to move my controller layer of MVC to the jQuery/javascript frontend and use pure JSON/REST to talk with the backend. Though the backend will need strong security and, for my application some ability to do workflow, queries, and rules.
You also might want to look at:
1) Couldkit, which runs on Tokyo Cabinet. Supports JSONQuery and OAuth. Runs on Ruby/Rack may have enough functionality. Loks like a strong REST implementation.
2) Persevere, which is Java based and strongly supported in Dojo. It is REST-ish but also has some RPC type calls. Seems very powerful overall, with server-side java scripting, etc.
I wouldn't mind hearing how you're coming along.
Cheers,
Alex
I wrote a gem called Rack::JSON for exactly this purpose, it acts as a basic REST interface to a MongoDB db. It was inspired by Cloudkit and supports JSONQuery and also runs on Ruby/Rack. It allows you to store and then access/query JSON documents.

Categories