Get all updates of Java4s
Follow Us: Email Newsletter

Spring Boot – Creating a RESTful Web Service Example

In the previous article we have just created a simple hello world spring boot application, in this tutorial I am going to show you how to create a Restful web service using Spring Boot, believe me its very simple :-)

Lets start with the directory structure of the project. (if you want to know how to create a simple spring boot project, you can go back to the previous article Spring Boot + Maven – Hello World Example Step by Step)

Directory Structure

 Required files

  • SpringBootApp.java
  • SpringJava4sController.java
  • pom.xml

pom.xml

SpringBootApp.java

SpringJava4sController.java

Explanation

Friends, I took previous spring boot hello world application and just added the SpringJava4sController class and written RESTful web service related logic. I haven’t added any new dependencies nor written any XML’s, rather added a simple java class. My REST class is annotated with @RestController, which tells Spring Boot to consider this class as REST controller and register @RequestMapping paths inside it to respond to the HTTP requests.

Note: Spring Boot will use 8080 as default tomcat port

Run the application and hit http://localhost:8080  [ This is for @RequestMapping(“/”) ]

 

Now try hitting  http://localhost:8080/hello   [ This is for @RequestMapping(“/hello”) ]

 

Note:  Have you clearly observed the above directory structure? I have created Spring Boot main application class in com.java4s.app and controller class in com.java4s.app.controller, and in my controller class I have written my RESTful service logic and was able to execute the application successfully. How spring boot knows to scan our controller? As we have created our main class in com.java4s.app package, while starting our application, it will scan all the components under that package.  As we have created our controller class in com.java4s.app.controller which is inside com.java4s.app, our controller was registered by spring boot.

If you create the controller class outside of the main package, lets say com.java4s.controllerIf you run the application it gives 404 error, just give a try and see :-)

What’s the solution for this?  we have to add @ComponentScan annotation in our Spring Boot main class, something like this..

That’s it friends, hope you enjoyed this article :-)

 

You Might Also Like

  ♦ About Author ♦

Java4s_Author
sivateja@java4s.com
Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

2 Responses to “Spring Boot – Creating a RESTful Web Service Example”
  1. Chetan bhandari says:

    Hi Sir,

    Your way of explanation is too good, i always refer this side to recall any toping which has been discuss in this turorial.
    I have request you to provide detail tutorial with more example on Spring Rest, Sprint Boot.
    And also please provide some Java 8 Concepts.

  2. Venkatesh says:

    Hi sir,

    Can u provide spring+rest services+hibernate CRUD operation.

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful LinksCurrent & UpComing Tutorials
Upcoming
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2017 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.