Spring 启动 @ApiController 注释不起作用

Spring Boot @ApiController annotation not working

我正在开发我的第一个 Spring-Boot 应用程序。在下面实现了一个有效的 UI 控制器:

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.http.HttpStatus;

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

@Controller
public class UiController {

    private ProductService productService;
    private LocationService locationService;
    private InventoryService inventoryService;
    private CartService cartService;


    public UiController(
            ProductService productService,
            LocationService locationService,
            InventoryService inventoryService,
            CartService cartService) {
        this.productService = productService;
        this.locationService = locationService;
        this.inventoryService = inventoryService;
        this.cartService = cartService;

    }

    @GetMapping("/")
    public String home(Model model) {
        model.addAttribute("products", productService.getAllProducts());
        return "index";
    }
    @GetMapping("/brand/{brand}")
    public String brand(Model model, @PathVariable String brand) {
        List prods = productService.getProductByBrand(brand);
        if (prods.size() == 0) throw new ItemNotFoundException();
        model.addAttribute("products", prods);
        return "index";
    }

    @ResponseStatus(value=HttpStatus.NOT_FOUND, reason="No such item")  // 404
    public class ItemNotFoundException extends RuntimeException {
        // ...
    }

    @GetMapping("/product/{productId}")
    public String product(Model model, @PathVariable String productId) {
        Product prod = productService.getProduct(productId);
        if (prod == null) throw new ItemNotFoundException();
        ArrayList<Product> ps = new ArrayList<Product>();
        ps.add(prod);
        model.addAttribute("products", ps);
        return "index";
    }
}

我想添加一个 REST 控制器 return 与 HTML 一样,只是我希望响应在 JSON 中。没有数据的时候,我要报错return。添加了以下内容:

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.HttpStatus;

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

import com.google.gson.Gson;

@ApiController
public class ApiController {

    private ProductService productService;
    private LocationService locationService;
    private InventoryService inventoryService;
    private CartService cartService;


    public ApiController(
            ProductService productService,
            LocationService locationService,
            InventoryService inventoryService,
            CartService cartService) {
        this.productService = productService;
        this.locationService = locationService;
        this.inventoryService = inventoryService;
        this.cartService = cartService;

    }

    @GetMapping("/rest")
    public String home() {
        List prods = productService.getAllProducts();
        if (prods.size() == 0) throw new ItemNotFoundException();
        return new Gson().toJson(prods);
    }

    @GetMapping("/rest/brand/{brand}")
    public String brand(@PathVariable String brand) {
        List prods = productService.getProductByBrand(brand);
        if (prods.size() == 0) throw new ItemNotFoundException();
        return new Gson().toJson(prods);
    }

    @ResponseStatus(value=HttpStatus.NOT_FOUND, reason="No such item")  // 404
    public class ItemNotFoundException extends RuntimeException {
        // ...
    }

    @GetMapping("/rest/product/{productId}")
    public String product(@PathVariable String productId) {
        Product prod = productService.getProduct(productId);
        if (prod == null) throw new ItemNotFoundException();
        return new Gson().toJson(prod);
    }
}

显然,autoconfig 正在运行,编译器选择了我的控制器。只是,我收到以下错误:

Compilation failure
ApiController.java:[21,2] incompatible types: com.rei.interview.ui.ApiController cannot be converted to java.lang.annotation.Annotation

我做错了什么,我该怎么办?

你在控制器的开头犯了一个简单的错误。 class 必须注释为 @RestController... 而不是 @ApiController

更改您的代码
@ApiController
public class ApiController {
   ...
}

@RestController  // <- Change annotation here
public class ApiController {
   ...
}

错误

ApiController.java:[21,2] incompatible types: 
com.rei.interview.ui.ApiController cannot be converted to java.lang.annotation.Annotation

通知您注解 @ApiController 不是 java.lang.annotation.Annotation

类型