Introduction to RESTful Web Services
Introduction to RESTful Web Services
Spring Boot is a project that is built on the top of the Spring Framework. It
provides an easier and faster way to set up, configure, and run both
simple and web-based applications.
We can use Spring STS IDE or Spring Initializr to develop Spring Boot
Java applications.
Along with the Spring Boot Framework, many other Spring sister projects
help to build applications addressing modern business needs. There are
the following Spring sister projects are as follows:
o Spring Data: It simplifies data access from the relational
and NoSQL databases.
o Spring Batch: It provides powerful batch processing.
o Spring Security: It is a security framework that provides
robust security to applications.
o Spring Social: It supports integration with social networking like
LinkedIn.
o Spring Integration: It is an implementation of Enterprise
Integration Patterns. It facilitates integration with other enterprise
applications using lightweight messaging and declarative
adapters.
o Java 1.8
o Maven 3.0+
o Spring Framework 5.0.0.BUILD-SNAPSHOT
o An IDE (Spring Tool Suite) is recommended.
Web Development
Admin Support
Spring Boot provides the facility to enable admin-related features for the
application. It is used to access and manage applications remotely. We
can enable it in the Spring Boot application by
using spring.application.admin.enabled property.
Externalized Configuration
Properties Files
YAML Support
Type-safe Configuration
The strong type-safe configuration is provided to govern and validate the
configuration of the application. Application configuration is always a
crucial task which should be type-safe. We can also use annotation
provided by this library.
Logging
Spring Boot uses Common logging for all internal logging. Logging
dependencies are managed by default. We should not change logging
dependencies if no customization is needed.
Security
Spring Boot applications are spring bases web applications. So, it is secure
by default with basic authentication on all HTTP endpoints. A rich set of
Endpoints is available to develop a secure Spring Boot application.
Prerequisite
Before learning Spring Boot, you must have the basic knowledge of Spring
Framework.
Audience
Our Spring Boot Tutorial is designed to help beginners and professionals.
Problem
We assure you that you will not find any problem with the Spring Boot
Tutorial. But if there is any mistake, please post the problem in the
contact form.
It does not define the standard message exchange format. We can build
REST services with both XML and JSON. JSON is more popular format with
REST. The key abstraction is a resource in REST. A resource can be
anything. It can be accessed through a Uniform Resource Identifier
(URI). For example:
The resource has representations like XML, HTML, and JSON. The current
state capture by representational resource. When we request a resource,
we provide the representation of the resource. The important methods of
HTTP are:
The primary comparison between Spring and Spring Boot are discussed
below:
It aims to simplify Java EE It aims to shorten the code length and provide
development that makes the easiest way to develop Web Applications.
developers more productive.
The primary feature of the The primary feature of Spring Boot
Spring Framework is Autoconfiguration. It automatically
is dependency injection. configures the classes based on the requirement.
To test the Spring project, we Spring Boot offers embedded server such
need to set up the sever as Jetty and Tomcat, etc.
explicitly.
It does not provide support for It offers several plugins for working with an
an in-memory database. embedded and in-memory database such
as H2.
Developers manually define Spring Boot comes with the concept of starter in
dependencies for the Spring pom.xml file that internally takes care of
project in pom.xml. downloading the dependencies JARs based on
Spring Boot Requirement.
Spring MVC: Spring MVC is a Web MVC Framework for building web
applications. It contains a lot of configuration files for various capabilities.
It is an HTTP oriented web application development framework.
Spring Boot and Spring MVC exist for different purposes. The primary
comparison between Spring Boot and Spring MVC are discussed below:
It reduces development time and increases It takes more time to achieve the
productivity. same.
ADVERTISEMENT
o Presentation Layer
o Business Layer
o Persistence Layer
o Database Layer
Business Layer: The business layer handles all the business logic. It
consists of service classes and uses services provided by data access
layers. It also performs authorization and validation.
Step 3: Click on File menu -> New -> Spring Starter Project ->
If the Spring Starter Project is not enlisted, then click on Other at the
bottom of the menu. A dialog box appears on the screen. Type Spring
Starter Project in the Wizards text box and click on the Next button.
Step 4: provide the name, group, and package of the project. We have
provided:
Name: restful-web-services
Group: com.javatpoint
Package: com.javatpoint.server.main
1. <project xmlns="http://maven.apache.org/POM/4.0.0"
2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4. http://maven.apache.org/xsd/maven-4.0.0.xsd">
5. <modelVersion>4.0.0</modelVersion>
6. <parent>
7. <groupId>org.springframework.boot</groupId>
8. <artifactId>spring-boot-starter-parent</artifactId>
9. <version>2.1.8.RELEASE</version>
10. <relativePath/> <!-- lookup parent from repository -->
11. </parent>
12. <groupId>com.javatpoint</groupId>
13. <artifactId>restful-web-services</artifactId>
14. <version>0.0.1-SNAPSHOT</version>
15. <name>restful-web-services</name>
16. <description>Demo project for Spring Boot</description>
17. <properties>
18. <java.version>1.8</java.version>
19. </properties>
20. <dependencies>
21. <dependency>
22. <groupId>org.springframework.boot</groupId>
23. <artifactId>spring-boot-starter</artifactId>
24. </dependency>
25. <dependency>
26. <groupId>org.springframework.boot</groupId>
27. <artifactId>spring-boot-starter-activemq</artifactId>
28. </dependency>
29. <dependency>
30. <groupId>org.springframework.boot</groupId>
31. <artifactId>spring-boot-starter-web</artifactId>
32. </dependency>
33. <dependency>
34. <groupId>org.springframework.boot</groupId>
35. <artifactId>spring-boot-starter-tomcat</artifactId>
36. </dependency>
37. <dependency>
38. <groupId>org.springframework</groupId>
39. <artifactId>spring-webmvc</artifactId>
40. </dependency>
41. <!-- https://mvnrepository.com/artifact/
org.springframework.boot/spring-boot-devtools -->
42. <dependency>
43. <groupId>org.springframework.boot</groupId>
44. <artifactId>spring-boot-devtools</artifactId>
45. <scope>runtime</scope>
46. </dependency>
47. <!-- https://mvnrepository.com/artifact/
org.hibernate.javax.persistence/hibernate-jpa-2.1-api -->
48. <dependency>
49. <groupId>org.hibernate.javax.persistence</groupId>
50. <artifactId>hibernate-jpa-2.1-api</artifactId>
51. <version>1.0.0.Final</version>
52. </dependency>
53. <!-- https://mvnrepository.com/artifact/com.h2database/h2 --
>
54. <dependency>
55. <groupId>com.h2database</groupId>
56. <artifactId>h2</artifactId>
57. <scope>runtime</scope>
58. </dependency>
59. <dependency>
60. <groupId>org.apache.maven</groupId>
61. <artifactId>maven-archiver</artifactId>
62. <version>2.5</version>
63. </dependency>
64. <dependency>
65. <groupId>org.springframework.boot</groupId>
66. <artifactId>spring-boot-starter-test</artifactId>
67. <scope>test</scope>
68. </dependency>
69. </dependencies>
70. <build>
71. <plugins>
72. <plugin>
73. <groupId>org.springframework.boot</groupId>
74. <artifactId>spring-boot-maven-plugin</artifactId>
75. </plugin>
76. </plugins>
77. </build>
78. </project>
1. package com.javatpoint.server.main;
2. import org.springframework.boot.SpringApplication;
3. import org.springframework.boot.autoconfigure.SpringBootApplicati
on;
4. @SpringBootApplication
5. public class RestfulWebServicesApplication
6. {
7. public static void main(String[] args)
8. {
9. SpringApplication.run(RestfulWebServicesApplication.class, args);
10. }
11. }
It does not perform any service but ensures that the application is running
properly.
Output
1. package com.javatpoint.server.main;
2. import org.springframework.web.bind.annotation.RequestMapping;
3. import org.springframework.web.bind.annotation.RequestMethod;
4. import org.springframework.web.bind.annotation.RestController;
5. //Controller
6. @RestController
7. public class HelloWorldController
8. {
9. //using get method and hello-world as URI
10. @RequestMapping(method=RequestMethod.GET, path="/
hello-world")
11. public String helloWorld()
12. {
13. return "Hello World";
14. }
15. }
1. package com.javatpoint.server.main;
2. import org.springframework.web.bind.annotation.GetMapping;
3. import org.springframework.web.bind.annotation.RestController;
4. //Controller
5. @RestController
6. public class HelloWorldController
7. {
8. //using get method and hello-world as URI
9. @GetMapping(path="/hello-world")
10. public String helloWorld()
11. {
12. return "Hello World";
13. }
14. }
HelloWorldController.java
1. package com.javatpoint.server.main;
2. import org.springframework.web.bind.annotation.GetMapping;
3. import org.springframework.web.bind.annotation.RestController;
4. //Controller
5. @RestController
6. public class HelloWorldController
7. {
8. //using get method and hello-world URI
9. @GetMapping(path="/hello-world")
10. public String helloWorld()
11. {
12. return "Hello World";
13. }
14. @GetMapping(path="/hello-world-bean")
15. public HelloWorldBean helloWorldBean()
16. {
17. return new HelloWorldBean("Hello World"); //constructor of H
elloWorldBean
18. }
19. }
Step 2: Create a class HelloWorldBean.
Right-click -> Source -> Generate Getters and Setters -> check the box ->
Ok
HelloWorldBean.java
1. package com.javatpoint.server.main;
2. public class HelloWorldBean
3. {
4. public String message;
5. //constructor of HelloWorldBean
6. public HelloWorldBean(String message)
7. {
8. this.message=message;
9. }
10. //generating getters and setters
11. public String getMessage()
12. {
13. return message;
14. }
15. public void setMessage(String message)
16. {
17. this.message = message;
18. }
19. @Override
20. //generate toString
21. public String toString()
22. {
23. return String.format ("HelloWorldBean [message=%s]", mess
age);
24. }
25. }
Step 5: Launch the HelloWorldController. The URL of the browser
changes to localhost:8080/hello-world-bean.
1. {
2. message: "Hello World"
3. }