我是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
答案 0 :(得分:0)
这是不可能的。强烈建议不要使用overload methods in Spring controller,它们具有相同路径的相同映射。
在实践中,对于每个不同的路径,您不应多次使用任何类型的HTTP动词。
答案 1 :(得分:0)
您可以使用“访问者模式”。此解决方案需要一个 PATCH 方法,参数中包含一个抽象资源。
@PatchMapping(value = "/foo/{id}", consumes = "application/json")
@Secured("MY_ROLE")
public void patch(@PathVariable("id") Long id, @RequestBody @Valid Resource patch) {
//in a service you can do that
patch.accept(new StateVisitor() {
@Override
public void visit(Foo resource) {
//do job ...
}
@Override
public void visit(Bar resource) {
//do job....
}
//...
});
}
}
例如
@Getter
@Setter
public class Foo extends Resource {
@NotNull
private ArchiveState archiveState;
@Override
public void accept(StateVisitor visitor) {
visitor.visit(this);
}
}
抽象类
@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Foo.class, name = "fooKey"),
@JsonSubTypes.Type(value = Bar.class, name = "barKey")
})
public abstract class Resource implements Visitable<StateVisitor> {
}
界面可访问
public interface Visitable<T> {
void accept(T visitor);
}
国家访问者
public interface StateVisitor {
void visit(Foo resource);
void visit(Bar resource);
}
在您的 json juste 中指出帮助 jackson 映射正确类的关键。这就是诀窍:)
希望对您有所帮助:)