首页 > 解决方案 > 如何在单个控制器中制作多个@PatchMapping?

问题描述

我是 Spring 和 REST API 本身的新手,我想在单个控制器中为我的实体“Employee”的多个属性制作一个 PatchMapper,但我收到此错误:

“引起:java.lang.IllegalStateException:不明确的映射。无法将'employeeController'方法公共resttest.testexercise.model.Employee resttest.testexercise.controller.EmployeeController.patchLN(java.util.Map,java.lang.Integer)映射到{PATCH /api/employees/{id}}:已经有 'employeeController' bean 方法”

我想不出在单个实体控制器中修补映射多个属性的方法,而且我只看到了修补映射单个属性的答案。有没有办法做到这一点?任何帮助将不胜感激!

package resttest.testexercise.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import resttest.testexercise.exception.EmployeeNotFoundException;
import resttest.testexercise.exception.EmployeeUnSupportedFieldPatchException;
import resttest.testexercise.exception.ResourceNotFoundException;
import resttest.testexercise.model.Employee;
import resttest.testexercise.repository.EmployeeRepository;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class EmployeeController {

    @Autowired
    EmployeeRepository employeeRepository;

    @PatchMapping("/employees/{id}")
    public Employee patchFN(@RequestBody Map<String, String> update, @PathVariable(value = "id") Integer id) {

        return employeeRepository.findById(id)
                .map(x -> {

                    String first_name = update.get("first_name");
                    if (!StringUtils.isEmpty(first_name)) {
                        x.setFirst_name(first_name);

                        return employeeRepository.save(x);
                    } else {
                        throw new EmployeeUnSupportedFieldPatchException(update.keySet());
                    }

                })
                .orElseGet(() -> {
                    throw new EmployeeNotFoundException(id);
                });

    }

    @PatchMapping("/employees/{id}")
    public Employee patchLN(@RequestBody Map<String, String> update, @PathVariable(value = "id") Integer id) {

        return employeeRepository.findById(id)
                .map(x -> {

                    String last_name = update.get("last_name");
                    if (!StringUtils.isEmpty(last_name)) {
                        x.setLast_name(last_name);

                        return employeeRepository.save(x);
                    } else {
                        throw new EmployeeUnSupportedFieldPatchException(update.keySet());
                    }

                })
                .orElseGet(() -> {
                    throw new EmployeeNotFoundException(id);
                });

    }

    @PatchMapping("/employees/{id}")
    public Employee patchBDay(@RequestBody Map<String, String> update, @PathVariable(value = "id") Integer id) {

        return employeeRepository.findById(id)
                .map(x -> {

                    String birthday = update.get("birthday");
                    if (!StringUtils.isEmpty(birthday)) {
                        x.setBirthday(birthday);

                        return employeeRepository.save(x);
                    } else {
                        throw new EmployeeUnSupportedFieldPatchException(update.keySet());
                    }

                })
                .orElseGet(() -> {
                    throw new EmployeeNotFoundException(id);
                });

    }

    @PatchMapping("/employees/{id}")
    public Employee patchAdd(@RequestBody Map<String, String> update, @PathVariable(value = "id") Integer id) {

        return employeeRepository.findById(id)
                .map(x -> {

                    String address = update.get("address");
                    if (!StringUtils.isEmpty(address)) {
                        x.setAddress(address);

                        return employeeRepository.save(x);
                    } else {
                        throw new EmployeeUnSupportedFieldPatchException(update.keySet());
                    }

                })
                .orElseGet(() -> {
                    throw new EmployeeNotFoundException(id);
                });

    }

    @PatchMapping("/employees/{id}")
    public Employee patchPos(@RequestBody Map<String, String> update, @PathVariable(value = "id") Integer id) {

        return employeeRepository.findById(id)
                .map(x -> {

                    String position = update.get("position");
                    if (!StringUtils.isEmpty(position)) {
                        x.setPosition(position);

                        return employeeRepository.save(x);
                    } else {
                        throw new EmployeeUnSupportedFieldPatchException(update.keySet());
                    }

                })
                .orElseGet(() -> {
                    throw new EmployeeNotFoundException(id);
                });

    }


}

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'employeeController' method 
public resttest.testexercise.model.Employee resttest.testexercise.controller.EmployeeController.patchLN(java.util.Map<java.lang.String, java.lang.String>,java.lang.Integer)
to {PATCH /api/employees/{id}}: There is already 'employeeController' bean method
public resttest.testexercise.model.Employee resttest.testexercise.controller.EmployeeController.patchFN(java.util.Map<java.lang.String, java.lang.String>,java.lang.Integer) mapped.
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1778) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at resttest.testexercise.TestexerciseApplication.main(TestexerciseApplication.java:14) ~[classes/:na]
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'employeeController' method 
public resttest.testexercise.model.Employee resttest.testexercise.controller.EmployeeController.patchLN(java.util.Map<java.lang.String, java.lang.String>,java.lang.Integer)
to {PATCH /api/employees/{id}}: There is already 'employeeController' bean method
public resttest.testexercise.model.Employee resttest.testexercise.controller.EmployeeController.patchFN(java.util.Map<java.lang.String, java.lang.String>,java.lang.Integer) mapped.
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.assertUniqueMethodMapping(AbstractHandlerMethodMapping.java:618) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.register(AbstractHandlerMethodMapping.java:586) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:312) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lambda$detectHandlerMethods$1(AbstractHandlerMethodMapping.java:282) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) ~[na:na]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:280) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.processCandidateBean(AbstractHandlerMethodMapping.java:252) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:211) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:199) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:164) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1837) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1774) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    ... 16 common frames omitted


Process finished with exit code 1

标签: javaspringhibernatespring-bootspring-mvc

解决方案


这是不可能的。您只能在 Spring 控制器中重载具有相同路径的相同映射的方法,尽管非常不鼓励这样做。

在实践中,您不应该对每个不同的路径多次使用任何类型的 HTTP 动词。


推荐阅读