Я новичок в Spring и REST API, и я хотел бы создать PatchMapper для нескольких свойств моей сущности "Сотрудник" в одном контроллере, но я получаю эту ошибку:
"Причина: java.lang.IllegalStateException: неоднозначное отображение. Невозможно отобразить метод employeeController
public resttest.testexercise.model.Employee resttest.testexercise.controller.EmployeeController.patchLN (java.util.Map, java.lang.Integer)
to {PATCH / api / employee / {id}}: метод бина employeeController уже существует "
Я не мог придумать способ исправления нескольких свойств карты в одном контроллере сущности, и я видел только ответы на одно свойство карты исправлений. Есть ли способ сделать это? Любая помощь будет принята с благодарностью!
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