Build an Web UI with AngularJS for a java project? - java

I would like to add a Web User Interface for an existing Java project I did time ago. I learned basics of AngularJS on codeschool.com courses and now I know how to send an http or REST request to get data for my web UI.
My Java project has a set of methods that elaborate some data from a local database and return integers or integers arrays.
The goal of the interface would be to show a bunch of charts and data directly from that Java project.
What would be the most appropriate way to do this? I heard of implementing REST services on my Java project but it seems overkill for the purpose and i'm totally confused by all the frameworks for this. What would you use?
Thanks everyone for your answers!

I would use SprinvMVC to provide data from server to client side.
Here is my project from which you can start and learn basics :
https://github.com/xwid/thaimaster
Basicly you should create spring controllers mapped to urls, by doing it this way, you will be able to retrive server data using angular js.
http://www.javabeat.net/spring-mvc-angularjs-integration/

If you don't want to use a full REST framework such as Jersey, another possibility would be to use an embeddable HTTP server (there are several) and handle the requests yourself. This would mean that requests to something like /myapp/ would return your AngularJS filled HTML page, and requests to /resources/* would provide with REST functionality.
This would give you a standalone Java program that doesn't need a servlet container, but it would be a somewhat hacky solution. Not production grade, but you'd probably learn something from having to handle the HTTP traffic yourself.

With the Spring Framework's SprigBoot, it's quite easy to implement a REST service and have a runnable java application. You can follow this Building a RESTful Web Service guide from spring.io. They are clear and quickly understandable.
Also if you are not already familiar, through these guides you can have a glimpse of gradle and maven as well.

Related

Spring boot back end with Swing client

I have a design question: I use DB + Hibernate + SpringBoot as back end in a server and use Swing clients on desktops.
I am considering for a new project the following architecture:
Back end (maybe in separate servers):
SQL DB
Spring Boot + Hibernate Java App
Front end(s):
Swing Java App
later: mobile App; Browser
I have searched a bit, but still have some doubts and feel I am missing something. I initially considered using React for front end. But this is going to take too long for me and I have lots of Swing components that I already optimized (and this my applications tend to use lots of grids with edits "in place")
I searched for Spring + Swing, but what I found was about running everything on the same JVM. I would think that separating them would allow me to (later) build a Mobile or Web front end using the same back end.
To sum it up:
create back end services with spring boot + hibernate on top of SQL db
create Swing front end that consumes those services
(later stage) mobile app to consume these same services
Questions:
Is this a good approach ? Am I seeing this the wrong way
How to "share" the POJOs between the back end and the front end? (thinking about placing the models on a separate lib); I don't mean how to transport them, I mean how to reuse them on both projects.
This is possible.
I would create the backend application with Spring Boot and access a datastore with Spring DATA. I would make services available via REST.
To access REST from your Swing client you must use a REST client of some sort. I would recommend Spring RestTemplate but there are many alternatives.
Synchronous client to perform HTTP requests, exposing a simple,
template method API over underlying HTTP client libraries such as the
JDK HttpURLConnection, Apache HttpComponents, and others.
Since you want mobile support some time in the future you could consider creating a webclient instead and making it cross platform (browser only).
To answer your two questions:
Regarding backend you will be perfectly fine as Spring Boot is a good option. As for the client I personally would not choose Swing today since it is very old and rather go for JavaFX 2 or a web client if that is an option. If you are the only developer and since you are proficient it might be a good solution for you.
You can share the POJOs from you Server for your Client as an api that you include as dependency. However it is usually not recommended to share POJOs but rather generate it on the client side or just type them. In your case if it is a small project it probably is ok. Do what is most easy for you.
I would recommend start using Maven as a build tool if you do not already.
Good luck.
Your request is perfectly valid, as you are a Swing developer and your approach is absolutely fine.
So let's draw the general picture: you're building a backend/frontend product/software.
Backend
Your backend will be a Spring application that will run on a server, let's say http://localhost:8080, serving requests to clients.
You'd probably want to create a #RestController on http://localhost:8080/api/... to expose your functions to your Swing client.
Swing client
Your client will be a Swing application running on your desktop.
To connect your client to the Spring application, you now need to implement a service in your Swing application that will call your Spring web server and fetch the resources from there.
To achieve this goal, may options exist:
Why not plain Java ? Use this tutorial to call your Spring services.
Http components from Apache are quite common.
Spring your app ! Spring API includes RestTemplate that you can use in your Swing app to call Spring backend and fetch resources for your desktop app.
As you can see, this is EXACTLY the same thing as with a website in React:
your web page is the JFrame
your JSX components in React would be your JPanels and components
you'd connect action listeners on components (JComboBox, JButton) to a service calling your Spring backend.
you'd then use the resources fetched from your Spring server to update your application state.
rince and repeat
So, be confident, your approach is correct. You'll find people ranting at you having chosen an obsolete techno but really, who cares?
(May I add, totally a pun, you've been smarter than the JavaFX guys which are now facing the segregation of javafx libraries from the core Java ;-))
And as always... happy coding !

REST API with websocket using Spring boot

I am actually working to my university junior project.
I want to make a game using spring boot, this game will need a WebSocket.
I will surely need to make a REST API for user registration, deleting, updating, posting and getting data.
Am I going to be able to use WebSocket along with REST?
If yes please send me resources that can help me, otherwise please tell me what should I do.
I'm sorry if my question was not so smart but I am new to all of this.
Yes, your Spring application can easily use both WebSocket and REST APIs. If you're going to use Spring Boot, I'd suggest to take a look into their example projects (note the spring-boot-sample-websocket- directories). I think it's the best way to learn.
More on WebSockets, I'd suggest to use STOMP protocol. There is also a nice guide for it on how you can get started.
For your RESTful API, you'll be making simple controllers / services, there is tones of resources for this. You can also follow this guide to get started as well.
While Spring applications support both Websocket and REST API's individually, you can use Websocket connection to send requests to your rest API's too. Needs a little glitching tho, but I've made it work in this github repo. Using this library you can represent your rest-api's in a websocket based protocol.
I also made java (and springboot starter) for client.

Create json rest api without writing or integrating any code

I've 2 questions about Java Rest APIs
1'st:
I would like to create a Rest API with Java Servlets for using in the mobile applications(IOS and Android)
and before doing that I would like to clarify the rest api content. However as much as I search through google all I found was automatic API creators from your Java code. But what I want is different first I want to document our Json Rest API so mobile and server developers can start to work independently.
Do you know any tool for that?
2'nd:
What I want to achive in our java server application is simple. Get simple json requests from mobile clients and do some database query and respond back with simple json objects.
For achiving this do I need to use any additional Rest API framework such as spring mvc (or something else) or just using Java Servlets and parse the request in doPost method and respond it there
Which one do you suggest?
Thanks
Restlet Studio (http://restlet.com/technical-resources/restlet-studio) or APISpark (http://restlet.com/products/apispark/) can bring you what you expect.
Restlet Studio allows you to define / design you API with a Web IDE: resources and their methods, representations (exchanged data structures). You can then have access to corresponding Swagger content and generate online client SDKs and even server skeletons for your API (this is internally based on Swagger Codegen)...
To implement your RESTful applications, Restlet can help you. It's a Java REST framework to access and / or implement RESTful applications. Restlet can be used within a servlet container with its servlet extension (see this link https://github.com/restlet/restlet-tutorial/blob/master/modules/org.restlet.tutorial.markdown/02_Server_Side/04_Server_Deployment/02_Servlet_Deployment.md).
Hope it helps you.
Thierry
I aggree with Stephan comment.
With Spring Boot and Spring Data Rest, the only code you will need to write is the mapping between your DB and your DAO Entities.
To document your API before writing it, you can use in fact any regular tools.
REST APIs don't have anything specific compared to others APIs to be documented.
Much of the documentation of APIs I've written were using Ms-Word...
Swagger provide a Json syntax to document a REST API, but it is mainly useful when used with Swagger-Ui, which allow to request the deployed server dynamically. You can use Swagger for your documentation, but as there will be no already existing server, I'm not sure it is worth the cost of learning its syntax. The main benefits is to give you constraints (like predefined fields) to follow.
By the way, I think that writing a REST Api Mapping or just document it takes the same amount of time, so I'm not sure it is really worth making all the documentation in a stand-alone way.

How to create Single Page Application (SPA) in java with hibernate and MySQL

I'm trying to choose best set of tools to create Single Page Applications. I would like to be able to use Java and Hibernate on server side along with MySQL. But what about ajax layer of SPA?
Maybe I have entirely wrong idea about that and Java and Hibernate make no sense in this case? But than how to implement complex server side operations?
It's rather question of UI part. The page asks a server for pages data. Java + hibernate could be used to implement service (e.g. REST service which returns data in JSON format). You can use SpringMVC to implement the service.
AJAX calls the service and process obtained data.
You can use angularJS for creating Single Page Application. this is the best framework i found for SPA development so far and using in most of projects developing which needs SPA.
for sample application refer to github
and detail about angularJS

Best way for handle Read HTTPRequst post data on Restful api

What is the best way of save data using Restful web service without using Ajax? As a example I need to add a new Customer to the database using submit button.
What is the best way of transfer data format (text,json,xml) ?
How to read POST or GET data from HttpRequest object?
If you can please give me a example in java .
Thank you
I think you need to separate the concepts a bit. A "Restful Web Service" is a web service designed using REST principals, whereas AJAX is a set of technologies used often on the client side for asynchronous requests to multiple resources (without fully reloading the page). The web service really shouldn't care how the HTTP request is generated, just the contents of the HTTP request.
Now if you're concerned about writing a rest service in Java, I would highly recommend looking into JAX-RS and the reference implementation Jersey. There are lots of examples of how to get up and running. You can use MessageBodyReader implementations are to convert data from the HTTP request entity into Java objects.
Obviously this is not the only way to get started with writing a Restful web service in Java, but is one way.
It's very definitely worth your time to carefully study Richardson and Ruby's RESTful Web Services to learn the REST architectural style. In addition to #ach_l's recommendation to use Jersey, take a look at the Restlet Java framework, which is completely wonderful.

Categories