简体   繁体   中英

Using REST calls in Spring to expose service layer

I have an existing Service layer of Java code that I'd like to use in some REST calls. The way I'd like to do this is to have a user pass in a service ID in the URL, and then on the backend lookup the service and method (in a DB or config file) and call it. For example:

http://foobar.com/rest/car

When this URL is called, I would take the serviceId of "car" and call the CarService. I imagine I'd have a simple configuration:

car=com.foobar.services.CarService
house=com.foobar.services.HouseService
etc..

Is there a way to do this using Spring? One concern I have is not calling the service, but figuring out which method to call. If I had a call to http://foobar.com/services/car/red - how would I pass in the method parameter of 'red' and decide which method to call?

Here's an example of what this would look like in Java:

@RequestMapping(value = "{serviceId}")
@ResponseBody
public Object getMarshalledObject(@PathVariable String serviceId) {

    if ("car".equals(serviceId)) {
        return getCar();
    }
    throw new ServiceNotFoundException("Service ID not found.");
}

I would make separate controllers for each service, and have each controller delegate to its corresponding service after it extracted the relevant information from the request.

Due to the nature of @RequestMapping on controllers and their methods, this should be pretty easy:

@RequestMapping("/car")
class CarController {
    @Autowired
    private CarService service;

    @RequestMapping("/{color}")
    public Object getCarsByColor(@PathVariable String carColor) {
        return service.getCarsByColor(houseColor);
    }
}

@RequestMapping("/house")
class HouseController {
    @Autowired
    private HouseService service;

    @RequestMapping("/{houseId}")
    public Object getHouseById(@PathVariable int houseId) {
        return service.getHouseById(houseId);
    }
}

What we have here is two different controllers, with different services, that are mapped by the @RequestMapping that is applied to the class. Further, the controller methods are called by the remaining path elements from the url.

Instead of a simple properties file where you have this...

car=com.foobar.services.CarService
house=com.foobar.services.HouseService

...configure Spring (in the appropriate dispatch configuration file) to manage those beans for you:

<bean id="car" class="com.foobar.services.CarService" />
<bean id="house" class="com.foobar.services.HouseService" />

Assuming your service classes implement a common interface (for example, com.foobar.services.BaseService ), in your controller you can autowire them up like so:

@Autowired
@Qualifier("car")
private BaseService _carService;

@Autowired
@Qualifier("house")
private BaseService _houseService;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM