Design and implement restful Web Services based on spring
In this tutorial, you will use spring to create a restful network service that is productive.
Why use restful Web services?
From integration with Amazon Web services to aggregating multiple data sources, restful Web services comply with Roy Fielding's architectural style guidelines, providing simple, efficient WEB APIs that support a small number of API users from a few to millions.
You're going to implement a restful Web service that might be because:
- You are creating an API, and the client needs to be able to use it over the network.
- You want to open your organization's data to a wide range of customers through the network to use.
- You need to integrate your app with other apps in your organization, but you can't control the language, tools, or architecture used by other apps.
Or you're just curious to see what spring's restful Web service looks like. Whatever the reason, you've found the right place.
What you will create
Yummy Noodle Bar (gourmet noodle bar) is globalizing. It wants to provide a restful Web service to a range of business aggregation organizations, especially let's Nosh, a popular restaurant guide that introduces large businesses to small-scale noodles. You will expand the internal application of yummy Noodle bar, create a new public Web API to submit, track, cancel, and modify orders.
What do you need?
- Approximately 30 minutes per session. You should be able to work easily in your way at lunchtime during the week, through the entire tutorial (or do it all on weekends!). )
- Install build tool Gradle, version 1.6 or later.
- A copy of the code (available through Git or a downloaded zip file).
- You choose the IDE. Spring recommends spring Tool Suite, which is free to download.
Download and run the code
If you have downloaded the base code, you will find that each section of this tutorial is in a separate folder, numbered as one, and so on. There are also snippets of code: they are actually original, and each part has a separate complete code fragment.
The initial code set includes:
- A project layout
- This tutorial begins with the creation of the core Realms and event classes (above ' Yummy Noodle Bar application ')
- For some basic unit tests of these classes
The complete code for each part is based on the initial code, plus the implementation code at a point in the tutorial.
You can choose from the initial code set, or you can start with a complete set of code for a given section. Not every code file is shown in the tutorial, because that will slow things down. But the important thing is, like writing tests, creating restful services, and looking at objects in key areas.
There are many systems for building, and this tutorial uses Gradle and Gradle wrapper to build, so if you have a copy of the code on hand, you don't need to install anything to run the code.
For example, if you want to check the initial code set, you can do this:
- $ git clone https://github.com/spring-guides/tut-rest.git
- $ CD Tut-rest/initial
- $./gradlew Clean Test
There, you can browse build/reports/tests and see all the test results.
If you want to jump to section 6th, the last part of this tutorial, to run the Web app and its tests, you can:
- $ CD. /6/complete
- $./gradlew Tomcatrunwar
In the other console, enter:
- $./gradlew Test
Some chapters do not need to run the Web app to perform tests.
Yummy Noodle Bar application framework and core areas
The current architecture of the application is shown in the "Life preserver" diagram below:
The life preserver diagram is a tool for building applications that conform to the hexagonal architecture principle, sometimes referred to as ' Ports and Adapters ', which is described first by Alistair Cockburn. The life preserver diagram shows the core areas of your application and the areas of consolidation surrounding them that can be directly mapped to packages or components that you will use throughout the tutorial, so this is a great way to understand the things model.
Open the initial project and you will see that the above life preserver map is mapped to a different package in the Src/main/java/com/yummynoodleba directory.
In the top-level package Com.yummynoodlebar.core of the core application, the following is included:
- Domain. A component package that contains only the application core domain concepts. These classes are the universal language in the core domain (terminology in domain-driven design, which is suggested to refer to the explicit expression of the original-translator note in the link).
- Repository. Component that stores and obtains the current state of the system realm object.
- Event. Component that contains events that the realm can receive and handle.
- Service. Component that handles an executable operation when it receives an event.
Take some time to familiarize yourself with the components in each package. The testing of core domain components is in the src/test of the project. These tests will show you how to use these components.
RESTful Web Services Domain
RESTful Web Services can integrate your app with all the clients that might be using your service. As a result, Restfulf services exist in their own integration domain, beyond the core of your application, as shown in the following diagram, Restfulf can serve the entire life cycle of your application.
By integrating your application with the outside world, you need to consider some of the following design and implementation constraints:
- The RESTful service API is where you focus, and the core application architecture should not affect the design of the API.
- Components that implement restful services need to increase speed, which is more appropriate for more users to rely on your services.
- Your RESTful service components should not contain any core application logic, but they need to work with your application's core domain components to make it easier to coordinate the necessary functional service Interfaces
English Original: Designing and Implementing RESTful Web Services with Spring
Link: http://www.oschina.net/translate/designing-and-implementing-restful-web-services-with-spring
- Using Spring for unit testing
- Does Java EE 7 mean that the spring framework is obsolete?
- Spring 3.2 Three notable new features
- Accelerate Java application Development 1―spring debug Startup speed
- Start using Spring Data JPA
Design and implement restful Web Services (GO) based on spring