我用Swagger定义了我的springboot api,我如何在其他模块中使用它来实现控制器?

时间:2016-10-24 06:28:32

标签: spring spring-boot swagger swagger-ui java-api

以下是我的项目结构的截图。 spring-boot-stub模块是api,我想在模块服务器中使用它,这是一个spring boot应用程序。

enter image description here

api由Swagger生成,其中一个api如下:

ProductsApi.class:

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.wondergate.polaris.model.Product;
import java.util.List;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Api(
    value = "products",
    description = "the products API"
)
public interface ProductsApi {
    @ApiOperation(
        value = "",
        notes = "Get the list of products",
        response = Product.class,
        responseContainer = "List",
        tags = {}
    )
    @ApiResponses({        @ApiResponse(
            code = 200,
            message = "OK",
            response = Product.class
        )})
    @RequestMapping(
        value = {"/products"},
        produces = {"application/json"},
        consumes = {"application/json"},
        method = {RequestMethod.GET}
    )
    ResponseEntity<List<Product>> getProducts();
}

我在模块服务器中实现api,如下所示:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import io.wondergate.polaris.api.ProductsApi;
import io.wondergate.polaris.model.Product;
import io.wondergate.polaris.server.dao.ProductDao;
import io.wondergate.polaris.server.dao.ResourceInfoDao;
import io.wondergate.polaris.server.model.ResourceInfo;
import io.wondergate.polaris.server.model.ServerProduct;
import io.wondergate.polaris.server.utils.Convertor;

@Controller
public class ProductController implements ProductsApi {

    @Autowired
    private ProductDao productDao;
    @Autowired
    private ResourceInfoDao resourceInfoDao;

    @RequestMapping("/")
    @ResponseBody
    public String index() {
        return "Greetings from Spring Boot!";
    }

//    @RequestMapping("/products")
    public ResponseEntity<List<Product>> getProducts() {
        List<Product> productList = new ArrayList<Product>();
        Iterable<ServerProduct> allProducts = productDao.findAll();
        for (ServerProduct serverProduct : allProducts) {
            Iterable<ResourceInfo> resourceInfo = resourceInfoDao.findByProductId(serverProduct.getId());
            Product product = Convertor.serverProductToProduct(serverProduct, resourceInfo);
            productList.add(product);
        }
        ResponseEntity<List<Product>> res = new ResponseEntity<List<Product>>(productList, HttpStatus.OK);
        return res;
    }

}

但是,尽管可以成功编译和启动项目,但它无法按照我的意愿映射URL路由,例如。

enter image description here

那我怎么办呢?我想让api define模块和实现模块分开,这样当我修改api定义时,我不需要修改它的实现(例如,url route)。

1 个答案:

答案 0 :(得分:0)

我为这个问题感到抱歉。实际上,项目可以正确的方式映射URL路由。上述问题的真正原因是内容类型不匹配。