我是 Spring 和 REST API 本身的新手,我想在单个 Controller 中为我的实体“Employee”的多个属性创建一个 PatchMapper,但我收到此错误:
“由以下原因引起:java.lang.IllegalStateException:不明确的映射。无法映射“employeeController”方法 公共(public)resttest.testexercise.model.Employeeresttest.testexercise.controller.EmployeeController.patchLN(java.util.Map,java.lang.Integer) 到 {PATCH/api/employees/{id}}:已经有 'employeeController' bean 方法”
我想不出一种方法来在单个实体 Controller 中修补映射多个属性,并且我只看到了修补映射单个属性的答案。有办法做到吗?任何帮助将不胜感激!
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
最佳答案
这是不可能的事。您只能overload methods in Spring controller尽管强烈建议不要这样做,但它们对于相同的路径具有相同的映射。
实际上,对于每个不同的路径,您不应多次使用任何类型的 HTTP 动词。
关于java - 如何在单个 Controller 中制作多个@PatchMapping?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56765400/