Why does the Rest Controller give an error : "No adapter for handler"?

Why does the Rest Controller give an error : "No adapter for handler"?

我想做一个 spring 与 MongoDB 交互的简单示例。 我有产品型号:

@NoArgsConstructor
@ToString(exclude = {"id"})
public class Product {
    @Id
    private String id;
    private String name;
    private Integer price;
    private LocalDateTime localDateTime;

    public Product(String name, Integer price, LocalDateTime localDateTime) {
        this.name = name;
        this.price = price;
        this.localDateTime = localDateTime;
    }
}

用于使用 DB 的简单存储库和服务:

public interface productRepository extends MongoRepository<Product,String> {
    Product findByName(String name);
    List<Product> findByPrice(Integer price);
}

服务:

@AllArgsConstructor
@Service
public class productServiceImpl implements productService<Product>{

    productRepository repository;

    @Override
    public Product saveOrUpdateProduct(Product product) {
        return repository.save(product);
    }

    @Override
    public List<Product> findAll() {
        return repository.findAll();
    }

    @Override
    public Product findByName(String name) {
        return repository.findByName(name);
    }

    @Override
    public List<Product> findByPrice(Integer price) {
        return repository.findByPrice(price);
    }
}

当我检查 findAll 的工作时,一切正常。但是当使用 Rest Service 时:


@RestController("/products")
@AllArgsConstructor
public class productRestController {


    productServiceImpl productService;

    @GetMapping("/")
    public List<Product> getAllProducts(){
        System.out.println("*********************inside get all ***********************");
        return productService.findAll();
    }

    @GetMapping("/products/{name}")
    public Product getProductsByName(@PathVariable("name")Optional<String> name ){
        if(name.isPresent())
            return productService.findByName(name.get());
        else return null;
    }

    @GetMapping("/products/{price}")
    public List<Product> getProductsByPrice(@PathVariable("price")Optional<Integer> price ){
        if(price.isPresent())
            return productService.findByPrice(price.get());
        else return null;
    }

    @PostMapping("/save")
    public ResponseEntity<?> saveProduct(@RequestBody Product product){
        Product p = productService.saveOrUpdateProduct(product);
        return new ResponseEntity(p, HttpStatus.OK);
    }

}

并调用 http://localhost:8080/products/ 我得到一个错误:

No adapter for handler [com.example.MongoTesr.REST.productRestController@6e98d209]: 
The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler

我试过Google但是没有找到错误和解决方法problem.Can你告诉我我做错了什么?

application.propertires:

spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=root
spring.data.mongodb.password=rootpassword
spring.data.mongodb.database=test_db
spring.data.mongodb.port=27017
spring.data.mongodb.host=localhost

@RestController("/products")并没有按照你的想法去做!

/products 不是路径或映射到它或类似的,而只是:

The value may indicate a suggestion for a logical component name, to be turned into a Spring bean in case of an autodetected component.

来自 RestController.value javadoc.


如果我们想在 class 级别构建路径映射,我们可以通过以下方式实现:

...
@RestController  // resp. @Controller
@RequestMapping("/products") //(resp. relative: "products")
public class ...

另请参阅:Using @RequestMapping on class level