I am relatively new in terms of Java web development skills. I have a project that I think would make a good candidate for a RESTful service from what little I understand about APIs. I’m trying to get into the details of how this is supposed to be structured, but not really getting anywhere in terms of google searches and reading the material I already have. I’m hoping that this post will yield some validation and/or redirection in terms of my knowledge and assumptions on this topic.
My current assumption is that my RESTful service will have the following structure:
- Database Data (SQL).
- An ORM (I’m using a relatively unpopular ORM called CPO, but this would just be replaced with Hibernate with most people).
- A Java manager class with methods that talk to the ORM to obtain the data
- A Java controller class/classes that handles request mapping and uses
@ResponseBodyto direct/handle the URL and actions of how the data is handled via HTTP verbs (http://mysite.com/computers/dell might be
GETrequest with the word “dell” in the URL being a parameter that will return a JSON array of information about dell computers).
- This service should be made with Spring Boot, or somehow be able to stand alone and be independent of any other application.
Now assuming that the above is correct, then I’d have (on a very basic level) a RESTful service that any application can use to consume and use data.
So say I then have my web application. Let’s say I’m making a web app about computer hardware information, and I’m using Spring to build this web app. Here are my assumptions:
- This web app would also have it’s own controller to handle the app’s URL requests and routing, and the controller would then use, say, the
Am I on the right track, here? I understand that there is also an authentication aspect to RESTful services, but I’m not there yet conceptually (and my project is going to be used in a private network so security is not a priority at this point).
Any insight, criticism, knowledge, feedback, or clarification is greatly appreciated.
Here is one of my favorite kick-off examples of structure for your spring rest app.
1. Separation of layers, each layer is an individual module/project
- REST API
- Packaged as war (Could be jar if you are using spring boot with embedded server. Spring boot doc clearly explains how to deploy the so-call uber jar. It is deadly simple.)
- It has rest controllers that handle request/responses
- depends on Service Module below
- Packaged as jar
- Business logic abstractions, this layer has no idea how to communicate with datasource.
- It will be autowired in rest controllers
- Depends on DAO/Repository module below
- Packaged as jar
- Talks to data source directly, has operations commonly known as CRUD. It could be simple jdbc, JPA, or even file access.
- Depends on domain module below
- Packaged as jar
- It has your domain models, normally POJO classes. If you are using ORM, they are ORM entities.
- It could also have DTO (Data Transfer Object), which are still under hot debates. Use it or not is your call.
- You can add more modules such as utility, third party integration, etc.. but the above are highly recommended to have.
2. Build/Dependency Management Tools (Very necessary IMHO)
There are plenty of them, google search will show you. I personally like Maven with Spring. It simply works for the above project structure.
Also note that if you are using maven, there is a parent module that aggregates all the modules discussed in section 1. All the bullet point modules also corresponds to maven modules.
3. Thoughts on your particular project
If you insist on using JSP, you will need introduce another war(web app) that has controllers and JSPs. Controller will get the data(Normally Json/xml format) and then parse them to your models(POJO) so that your JSP can get them from your controller and do the display. Posting data from JSP is the reverse, I omitted here.
It is nowhere near a complete guide since this topic is quite huge and depends heavily on your specific requirement but the terms included here is enough for you to do additional research(Google, it is). Hopefully this will give you some ideas on how to approach.
While agreeing with most of the answer from @Minjun.Y, I think I would go take a slightly different approach to the REST and Web page layer. From my reading of your question, I think you are wanting to expose both a web interface and a REST interface to the outside world. There is little to be gained by reading POJOs from the database, turning the data into JSON, then back into POJOs for consumption by JSPs.
I would favor making the service layer do all the real work, and adding separate “presentation” layers for the web app (JSPs) and the REST controller. These would be separate controllers, into which the service would be injected. Alternatively, go with just a REST service and build all the presentation logic on the client side as per the previous answer.
Also, I am not a great fan of Maven modules. The way our Java shop would implement your project would be to make regular releases of the service layer, then make the presentation layers dependent on the latest release. There is room for discussion on this, but it certainly works for us. We would have the web interface and REST interfaces as separate Maven projects, as they normally live in different .war files and hence require separate deployment.
BTW, I would reinforce the need to get up to speed with build and dependency management tools. Once your project grows to any reasonable size, you need them. Free tools like Maven, Jenkins and Nexus make release management less of a problem.