简体   繁体   English

在Spring中使用REST调用公开服务层

[英]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. 我有一个现有的Java代码服务层,希望在某些REST调用中使用。 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. 我要这样做的方法是让用户在URL中传递服务ID,然后在后端查找服务和方法(在数据库或配置文件中)并调用它。 For example: 例如:

http://foobar.com/rest/car http://foobar.com/rest/car

When this URL is called, I would take the serviceId of "car" and call the CarService. 调用此URL时,我将使用“ car”的serviceId并调用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? 有没有办法使用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? 如果我有一个http://foobar.com/services/car/red的调用,我该如何传递'red'的方法参数并确定要调用的方法?

Here's an example of what this would look like in Java: 这是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的性质,这应该很简单:

@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. 我们这里拥有两个具有不同服务的不同控制器,这些控制器由应用于类的@RequestMapping映射。 Further, the controller methods are called by the remaining path elements from the url. 此外,控制器方法由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: ...配置Spring(在适当的调度配置文件中)为您管理那些bean:

<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: 假设您的服务类实现了一个公共接口(例如com.foobar.services.BaseService ),则在您的控制器中,您可以像这样自动将它们连接起来:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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