Java Spring RESTful Service Structure for Beginners

apijavarestspringweb-applications

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 @ResponseBody to direct/handle the URL and actions of how the data is handled via HTTP verbs (http://mysite.com/computers/dell might be GET request 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:

  • I'd have a bunch of views as JSPs, with the JSPs having HTML, CSS, and JavaScript includes. The JavaScript would handle AJAX calls to this application's controller as needed (below).
  • 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 ModelAndView object or something along those lines to "talk to" the RESTful service's controller, obtain whatever data is being passed, pass that data back to the view (Javascript, JSP, etc…) for display.

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.

Best Answer

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
  • Service
    • 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
  • DAO/Repository
    • 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
  • Domain
    • 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

Since you are using REST, I would highly recommend that you NOT use JSP as your view. You can use plain HTML5 + Javascript or some popular framework like AngularJS as your view.
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.

Related Topic