简体   繁体   English

Spring 引导:使用 BaseController 进行 CRUD 操作时如何防止 URL 覆盖

[英]Spring Boot: How to prevent URL override when using a BaseController for CRUD operations

I have a Spring Boot application and I have implemented a base controller that handles CRUD operations for all my entities.我有一个 Spring 启动应用程序,我已经实现了一个基础 controller 来处理我所有实体的 CRUD 操作。 I have also created a BrandController that extends the base controller and a BrandRepository that implements CrudRepository.我还创建了一个扩展基础 controller 的 BrandController 和一个实现 CrudRepository 的 BrandRepository。 The problem is that when I try to access the endpoints for the BrandController such as /api/brands, I get a 404 error, but I can access them on /brands How can I fix this so that the endpoints are accessible with /api/entitys?问题是,当我尝试访问 BrandController 的端点(例如 /api/brands)时,出现 404 错误,但我可以在 /brands 上访问它们 我该如何解决这个问题,以便可以使用 /api/ 访问端点实体?

Here is the code for the BrandController:这是品牌控制器的代码:

package parc.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import parc.model.concrete.Brand;
import parc.repository.BrandRepository;

@RestController
@RequestMapping("/api/brands")
public class BrandController extends BaseController<Brand, BrandRepository> {

    private final BrandRepository repository;

    public BrandController(BrandRepository repository) {
        super(repository);
        this.repository = repository;
    }

}

Here is the code for the BaseController:这是 BaseController 的代码:

package parc.controller;

import org.springframework.data.repository.CrudRepository;
import org.springframework.web.bind.annotation.*;

import java.util.List;

public class BaseController<T, R extends CrudRepository<T, Long>> {

    private R repository;

    public BaseController(R repository) {
        this.repository = repository;
    }

    @GetMapping("/")
    public List<T> getAll() {
        return (List<T>) repository.findAll();
    }

    @PostMapping("/")
    public T create(@RequestBody T entity) {
        return repository.save(entity);
    }

    @GetMapping("/{id}")
    public T getById(@PathVariable long id) {
        return repository.findById(id).orElse(null);
    }

    @PutMapping("/{id}")
    public T update(@PathVariable long id, @RequestBody T entity) {
        return repository.save(entity);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable long id) {
        repository.deleteById(id);
    }
}

And finally the code for the BrandRepository:最后是 BrandRepository 的代码:

package parc.repository;

import org.springframework.data.repository.CrudRepository;
import parc.model.concrete.Brand;

public interface BrandRepository extends CrudRepository<Brand, Long> {

}

I'm not a pro in Spring Boot so I'll appreciate any kind of help!我不是 Spring Boot 的专业人士,所以我将不胜感激任何帮助!

What do you have in application.yml?你在 application.yml 中有什么? maybe setting the following code will work:也许设置以下代码会起作用:

in application.yml:在 application.yml 中:

server:
    contextPath: /api

and the BrandController:和品牌控制器:

package parc.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import parc.model.concrete.Brand;
import parc.repository.BrandRepository;

@RestController
@RequestMapping("/brands")
public class BrandController extends BaseController<Brand, BrandRepository> {

    private final BrandRepository repository;

    public BrandController(BrandRepository repository) {
        super(repository);
        this.repository = repository;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM