Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.11 KB | None | 0 0
  1. package resttest.testexercise.controller;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.http.ResponseEntity;
  5. import org.springframework.util.StringUtils;
  6. import org.springframework.web.bind.annotation.*;
  7. import resttest.testexercise.exception.EmployeeNotFoundException;
  8. import resttest.testexercise.exception.EmployeeUnSupportedFieldPatchException;
  9. import resttest.testexercise.exception.ResourceNotFoundException;
  10. import resttest.testexercise.model.Employee;
  11. import resttest.testexercise.repository.EmployeeRepository;
  12.  
  13. import javax.validation.Valid;
  14. import java.util.List;
  15. import java.util.Map;
  16.  
  17. @RestController
  18. @RequestMapping("/api")
  19. public class EmployeeController {
  20.  
  21. @Autowired
  22. EmployeeRepository employeeRepository;
  23.  
  24. @PatchMapping("/employees/{id}")
  25. public Employee patchFN(@RequestBody Map<String, String> update, @PathVariable(value = "id") Integer id) {
  26.  
  27. return employeeRepository.findById(id)
  28. .map(x -> {
  29.  
  30. String first_name = update.get("first_name");
  31. if (!StringUtils.isEmpty(first_name)) {
  32. x.setFirst_name(first_name);
  33.  
  34. // better create a custom method to update a value = :newValue where id = :id
  35. return employeeRepository.save(x);
  36. } else {
  37. throw new EmployeeUnSupportedFieldPatchException(update.keySet());
  38. }
  39.  
  40. })
  41. .orElseGet(() -> {
  42. throw new EmployeeNotFoundException(id);
  43. });
  44.  
  45. }
  46.  
  47. @PatchMapping("/employees/{id}")
  48. public Employee patchLN(@RequestBody Map<String, String> update, @PathVariable(value = "id") Integer id) {
  49.  
  50. return employeeRepository.findById(id)
  51. .map(x -> {
  52.  
  53. String last_name = update.get("last_name");
  54. if (!StringUtils.isEmpty(last_name)) {
  55. x.setLast_name(last_name);
  56.  
  57. // better create a custom method to update a value = :newValue where id = :id
  58. return employeeRepository.save(x);
  59. } else {
  60. throw new EmployeeUnSupportedFieldPatchException(update.keySet());
  61. }
  62.  
  63. })
  64. .orElseGet(() -> {
  65. throw new EmployeeNotFoundException(id);
  66. });
  67.  
  68. }
  69.  
  70. @PatchMapping("/employees/{id}")
  71. public Employee patchBDay(@RequestBody Map<String, String> update, @PathVariable(value = "id") Integer id) {
  72.  
  73. return employeeRepository.findById(id)
  74. .map(x -> {
  75.  
  76. String birthday = update.get("birthday");
  77. if (!StringUtils.isEmpty(birthday)) {
  78. x.setBirthday(birthday);
  79.  
  80. // better create a custom method to update a value = :newValue where id = :id
  81. return employeeRepository.save(x);
  82. } else {
  83. throw new EmployeeUnSupportedFieldPatchException(update.keySet());
  84. }
  85.  
  86. })
  87. .orElseGet(() -> {
  88. throw new EmployeeNotFoundException(id);
  89. });
  90.  
  91. }
  92.  
  93. @PatchMapping("/employees/{id}")
  94. public Employee patchAdd(@RequestBody Map<String, String> update, @PathVariable(value = "id") Integer id) {
  95.  
  96. return employeeRepository.findById(id)
  97. .map(x -> {
  98.  
  99. String address = update.get("address");
  100. if (!StringUtils.isEmpty(address)) {
  101. x.setAddress(address);
  102.  
  103. // better create a custom method to update a value = :newValue where id = :id
  104. return employeeRepository.save(x);
  105. } else {
  106. throw new EmployeeUnSupportedFieldPatchException(update.keySet());
  107. }
  108.  
  109. })
  110. .orElseGet(() -> {
  111. throw new EmployeeNotFoundException(id);
  112. });
  113.  
  114. }
  115.  
  116. @PatchMapping("/employees/{id}")
  117. public Employee patchPos(@RequestBody Map<String, String> update, @PathVariable(value = "id") Integer id) {
  118.  
  119. return employeeRepository.findById(id)
  120. .map(x -> {
  121.  
  122. String position = update.get("position");
  123. if (!StringUtils.isEmpty(position)) {
  124. x.setPosition(position);
  125.  
  126. // better create a custom method to update a value = :newValue where id = :id
  127. return employeeRepository.save(x);
  128. } else {
  129. throw new EmployeeUnSupportedFieldPatchException(update.keySet());
  130. }
  131.  
  132. })
  133. .orElseGet(() -> {
  134. throw new EmployeeNotFoundException(id);
  135. });
  136.  
  137. }
  138.  
  139.  
  140. }
  141.  
  142. 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
  143. public resttest.testexercise.model.Employee resttest.testexercise.controller.EmployeeController.patchLN(java.util.Map<java.lang.String, java.lang.String>,java.lang.Integer)
  144. to {PATCH /api/employees/{id}}: There is already 'employeeController' bean method
  145. public resttest.testexercise.model.Employee resttest.testexercise.controller.EmployeeController.patchFN(java.util.Map<java.lang.String, java.lang.String>,java.lang.Integer) mapped.
  146. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1778) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
  147. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
  148. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
  149. at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
  150. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
  151. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
  152. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
  153. at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
  154. at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
  155. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
  156. at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
  157. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
  158. at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
  159. at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
  160. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
  161. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
  162. at resttest.testexercise.TestexerciseApplication.main(TestexerciseApplication.java:14) ~[classes/:na]
  163. Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'employeeController' method
  164. public resttest.testexercise.model.Employee resttest.testexercise.controller.EmployeeController.patchLN(java.util.Map<java.lang.String, java.lang.String>,java.lang.Integer)
  165. to {PATCH /api/employees/{id}}: There is already 'employeeController' bean method
  166. public resttest.testexercise.model.Employee resttest.testexercise.controller.EmployeeController.patchFN(java.util.Map<java.lang.String, java.lang.String>,java.lang.Integer) mapped.
  167. at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.assertUniqueMethodMapping(AbstractHandlerMethodMapping.java:618) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
  168. at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.register(AbstractHandlerMethodMapping.java:586) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
  169. at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:312) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
  170. at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lambda$detectHandlerMethods$1(AbstractHandlerMethodMapping.java:282) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
  171. at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) ~[na:na]
  172. at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:280) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
  173. at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.processCandidateBean(AbstractHandlerMethodMapping.java:252) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
  174. at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:211) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
  175. at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:199) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
  176. at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:164) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
  177. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1837) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
  178. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1774) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
  179. ... 16 common frames omitted
  180.  
  181.  
  182. Process finished with exit code 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement