Spring Boot @ApiController 注释不起作用

伊戈尔·施穆克勒

我正在开发我的第一个 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 控制器,返回与 HTML 相同的内容,但我希望响应为 JSON。当没有数据时,我想要一个错误返回。添加了以下内容:

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

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

大卫·洛伦佐·马里诺

您在控制器开始时犯了一个简单的错误。该类必须被注释@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不是 a 类型java.lang.annotation.Annotation

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章