Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Building a RESTful Web Service with Spring

You're reading from   Building a RESTful Web Service with Spring A hands-on guide to building an enterprise-grade, scalable RESTful web service using the Spring Framework

Arrow left icon
Product type Paperback
Published in Oct 2015
Publisher
ISBN-13 9781785285714
Length 128 pages
Edition 1st Edition
Languages
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Ludovic Dewailly Ludovic Dewailly
Author Profile Icon Ludovic Dewailly
Ludovic Dewailly
Arrow right icon
View More author details
Toc

Creating resources

The inventory component of our sample property management system deals with rooms. In Chapter 3, The First Endpoint, we built an endpoint to access rooms. Let's take a look at how to define an endpoint for creating new resources:

@RestController
@RequestMapping("/rooms")
public class RoomsResource {

  @RequestMapping(method = RequestMethod.POST)
  public ApiResponse addRoom(@RequestBody RoomDTO room) {
    Room newRoom = createRoom(room);
    return new ApiResponse(Status.OK, new RoomDTO(newRoom));
  }
}

We've added a new method to our RoomsResource class to handle the creation of new rooms. As described in Chapter 3, The First Endpoint, @RequestMapping is used to map requests to the Java method. Here, we map POST requests to addRoom().

Tip

Not specifying a value (path) in @RequestMapping is equivalent to using "/".

We pass the new room as a @RequestBody annotation. This annotation instructs Spring to map the body of the incoming web request...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image