13

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.

    2 Answers 2

    19

    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.

    8
    • 1
      Domain is usually the layer containing business logic. Domain is usually also refered to as the layer containing services. Domain objects are not plain POJOs, Domain object should contain business logic such as argument validation and as such. It would probably be better to rename the layer to something else. Repository layer is also quite often used to transfer data from multiple sources to your domain objects.
      – Andy
      CommentedJul 1, 2016 at 7:24
    • Will the Service and Repository modules be Spring projects as well?CommentedMar 6, 2017 at 17:28
    • 1
      @GlennVanSchil No, it does not have to be Spring projects b/c when the whole project is built, repo/service layer will be included in the classpath. The @Autowire will work as a result.CommentedMar 6, 2017 at 20:04
    • @MinjunYu Thanks for the clear answer! But your repo/service does need spring as maven dependency for the Service, Repository or Component annotations am I right?CommentedMar 7, 2017 at 7:18
    • 1
      @GlennVanSchil If you put all the spring maven dependencies in the pom.xml of the parent module, then there is not need to add any spring related dependencies in child modules (repo/service modules). This is just one way to layout multi modules projects in spring. The purpose is to organize your code. If your project is not that big and will not change in the foreseeable future, you can combine the domain,repo,service in the same module called "core". It looks even cleaner.CommentedMar 7, 2017 at 16:04
    2

    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.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.