如何在Spring启动应用程序中创建RestController的多个实例?

时间:2017-10-22 17:12:57

标签: spring spring-boot spring-restcontroller

我想创建不同的Spring RestController实例,并注入不同的bean实例(service,dao,cache)。

目前,我们已使用JAX-RS Restful API实现了此功能。

但是当我尝试在Spring RestController中实现相同的功能时,当Spring尝试创建一个新的bean“product2RestController”时,我遇到了错误。

因为bean“product1RestController”已经映射到其余URL并已创建。

堆栈跟踪:

在类路径资源中定义名称为'requestMappingHandlerMapping'的bean时出错[org / springframework / boot / autoconfigure / web / WebMvcAutoConfiguration $ EnableWebMvcConfiguration.class]:调用init方法失败;嵌套异常是java.lang.IllegalStateException:不明确的映射。无法映射'product2RestController'方法

@Path("/products")
public class ProductLookupRestService {

    @Context
    HttpServletRequest httpServletRequest;

    @Context
    HttpServletResponse httpServletResponse;

    @Context
    UriInfo uriInfo;

    private Map<Integer, AbstractProductRestService> productServiceLoopup;

    public void setProductServiceLoopup(Map<Integer, AbstractProductRestService> productServiceLoopup) {
        this.productServiceLoopup = productServiceLoopup;
    }

    @Path("/{productId}")
    public AbstractProductRestService getReport(@PathParam("productId") int productId) {
        AbstractProductRestService productRestService = this.productServiceLoopup.get(productId);
        productRestService.setHttpServletRequest(httpServletRequest);
        productRestService.setHttpServletResponse(httpServletResponse);
        productRestService.setUriInfo(uriInfo);

        return productRestService;
    }
}
public abstract class AbstractProductRestService {

    @Context
    protected HttpServletRequest httpServletRequest;

    @Context
    protected HttpServletResponse httpServletResponse;

    @Context
    protected UriInfo uriInfo;

    protected IProductService productService;
    protected IProductCache productCache;

    public void setHttpServletRequest(HttpServletRequest httpServletRequest) {
        this.httpServletRequest = httpServletRequest;
    }

    public void setHttpServletResponse(HttpServletResponse httpServletResponse) {
        this.httpServletResponse = httpServletResponse;
    }

    public void setUriInfo(UriInfo uriInfo) {
        this.uriInfo = uriInfo;
    }

    public void setProductService(IProductService productService) {
        this.productService = productService;
    }

    public void setProductCache(IProductCache productCache) {
        this.productCache = productCache;
    }

    @POST
    @Path("/filters")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public Response createFilters(Form filterForm) {
        //Save the filters and return uuid
        ResponseBuilder responseBuilder = Response.created(uriInfo.getAbsolutePathBuilder().path("uuid").build());

        return responseBuilder.build();
    }
}
public class ProductRestService extends AbstractProductRestService {

    @GET
    @Path("/filters/{uuid}/detail")
    public String getProductDetail(){
        // do cache check : productCache
        return this.productService.readProductDetail();
    }
}
public class ProductServiceImpl implements IProductService{

    @Override
    public String readProductDetail() {
        // Call to dao to get the data
        return null;
    }

}
<bean id="product1RestService" class="com.poc.jaxrs.rest.ProductRestService">
        <property name="productService" ref="product1Service"/>
        <property name="productCache" ref="product1Cache"/>
    </bean>

    <bean id="product2RestService" class="com.poc.jaxrs.rest.ProductRestService">
        <property name="productService" ref="product2Service"/>
        <property name="productCache" ref="product2Cache"/>
    </bean>

    <bean id="restProductLookupService" class="com.poc.jaxrs.rest.ProductLookupRestService">
        <property name="productServiceLoopup">
            <map key-type="java.lang.Integer">
                <entry key="1"><ref bean="product1RestService"/></entry>
                <entry key="2"><ref bean="product2RestService"/></entry>
            </map>
        </property>
    </bean>

我在下面使用urls来调用服务。

POST电话:http://localhost:8080/productservice/products/1/filters

GET Call:http://localhost:8080/productservice/products/1/filters/uuid/detail

在我的spring应用程序上下文xml中,我正在创建多个ProductRestService类实例,并注入了不同的bean(服务,缓存)实例。

我想创建Spring RestController的多个实例,但是我遇到了不明确的映射错误。

提前感谢您的帮助。

0 个答案:

没有答案