官术网_书友最值得收藏!

Creating a RESTful web service

In Spring Boot, all the HTTP requests are handled by controller classes. To be able to create a RESTful web service, first, we have to create a controller class. We will create our own Java package for our controller:

  1. Activate the root package in the Eclipse Project Explorer and right-click. Select NewPackage from the menu. We will name our new package com.packt.cardatabase.web:
  1. Next, we will create a new controller class in a new web package. Activate the com.packt.cardatabase.web package in the Eclipse project explorer and right-click. Select NewClass from the menu. We will name our class  CarController:
  1. Now, your project structure should look like the following screenshot:

If you create classes in a wrong package accidentally, you can drag and drop the files between packages in the Eclipse Project Explorer. Sometimes, the Eclipse Project Explorer view might not be rendered correctly when you make some changes. Refreshing the project explorer helps (Activate Project Explorer and press F5).

  1. Open your controller class in the editor window and add the @RestController annotation before the class definition. See the following source code. The @RestController annotation identifies that this class will be the controller for the RESTful web service:
      package com.packt.cardatabase.web;

import org.springframework.web.bind.annotation.RestController;

@RestController
public class CarController {
}
  1. Next, we add a new method inside our controller class. The method is annotated with the @RequestMapping annotation, which defines the endpoint that the method is mapped to. Following, you can see the sample source code. In this example, when a user navigates to the /cars endpoint, the getCars() method is executed: 
      package com.packt.cardatabase.web;

import org.springframework.web.bind.annotation.RestController;

@RestController
public class CarController {
@RequestMapping("/cars")
public Iterable<Car> getCars() {

}
}

The getCars() method returns all the car objects, which are then marshalled to JSON objects by Jackson library.

By default, @RequestMapping handles all the HTTP method (GET, PUT, POST, and more) requests. You can define which method is accepted with the following @RequestMapping("/cars", method=GET) parameter.  Now, this method handles only GET requests from the /cars endpoint.

  1. To be able to return cars from the database, we have to inject our CarRepository into the controller. Then, we can use the findAll() method that the repository provides to fetch all cars. The following source code shows the controller code:
      package com.packt.cardatabase.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.packt.cardatabase.domain.Car;
import com.packt.cardatabase.domain.CarRepository;

@RestController
public class CarController {
@Autowired
private CarRepository repository;

@RequestMapping("/cars")
public Iterable<Car> getCars() {
return repository.findAll();
}
}
  1. Now, we are ready to run our application and navigate to localhost:8080/cars. We can see that there is something wrong, and the application seems to be in an infinite loop. That happens due to our one-to-many relationship between the car and owner tables. So, what happens in practice—first, the car is serialized, and it contains an owner that is then serialized, and that, in turn, contains cars that are then serialized... and so on. To avoid this, we have to add the @JsonIgnore annotation to the cars field in the Owner class:
      // Owner.java

@OneToMany(cascade = CascadeType.ALL, mappedBy="owner")
@JsonIgnore
private List<Car> cars;
  1. Now, when you run the application and navigate to localhost:8080/cars , everything should go as expected and you will get all the cars from the database in JSON format, as shown in the following screenshot:

We have done our first RESTful web service, which return all the cars. Spring Boot provides a much more powerful way of creating RESTful Web Services and this is investigated in the next topic.

主站蜘蛛池模板: 井冈山市| 新余市| 宁国市| 江源县| 资阳市| 锡林浩特市| 峨边| 青岛市| 通海县| 汽车| 金山区| 宁阳县| 卢氏县| 宕昌县| 肥乡县| 利辛县| 阜新市| 龙川县| 茶陵县| 咸宁市| 宜昌市| 电白县| 都昌县| 黔东| 崇信县| 泸溪县| 泸溪县| 大连市| 长武县| 宁陕县| 兴业县| 杭锦旗| 雷山县| 磐石市| 突泉县| 贺兰县| 武川县| 蒙自县| 休宁县| 永川市| 穆棱市|