Hi I’m new in SpringBoot and I created this microservice that doesn’t work. when I try to run a get with postman I get this error ,can you help me?
"timestamp": "2022-09-29T15:24:02.783 00:00",
"status": 500,
"error": "Internal Server Error",
"trace": "org.springframework.data.crossstore.ChangeSetPersister$NotFoundException\r\n\tat com.poste.anagrafica.controller.UserController.listUserByRegistrationNumber(UserController.java:45)\r\n\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\r\n\tat java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n\tat java.base/java.lang.reflect.Method.invoke(Method.java:566)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)\r\n\tat org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1071)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:964)\r\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)\r\n\tat org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:655)\r\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:764)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)\r\n\tat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)\r\n\tat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)\r\n\tat org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)\r\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)\r\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)\r\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)\r\n\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)\r\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)\r\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)\r\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)\r\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360)\r\n\tat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)\r\n\tat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)\r\n\tat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:890)\r\n\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1789)\r\n\tat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)\r\n\tat org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)\r\n\tat org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)\r\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\r\n\tat java.base/java.lang.Thread.run(Thread.java:829)\r\n",
"message": "No message available",
"path": "/user/cerca/ciao"
}
I attach the classes
this is the database
the class entiy
@Entity
@Table(name = "USERS")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class User implements Serializable {
@Id
@Column(name = "user_id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "created_date")
private LocalDateTime createdDate;
@Column(name = "deleted", nullable = false)
private Long number;
@Column(name = "modified_date")
private LocalDateTime modifiedDate;
@Column(name = "birth_place")
private String birthPlace;
@Column(name = "birthday")
private Date birthDay;
@Column(name = "canNominate")
private Long canNominate;
@Column(name = "email")
private String email;
@Column(name = "firstName")
private String firstName;
@Column(name= "fiscalCode")
private String fiscalCode;
@Column(name = "hiringDate")
private Date hiringDate;
@Column(name ="last_name")
private String lastName;
@Column(name = "matricola")
private String matricola;
@Column(name = "position")
private String position;
@Column(name = "registration_number")
private String registrationNumber;
@Column(name = "replaced")
private Long replaced;
@Column(name = "terminationDate")
private Date terminationDate;
@Column(name = "user_status")
private String userStatus;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "application_role_id")
private Role applicationRoleID;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "company_id")
private Company companyID;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "direction_id")
private Direction directions ;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "level_id")
private Levels levelID;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "referent_id")
private User referentID;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "role_id")
private Role roleID;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "task_id")
private EmployeeTask taskID;
}
the controller class, for configure the endpoint and the necessary method
@RestController
@RequestMapping("/user")
@Log
public class UserController {
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
@Autowired
UserService userService;
@GetMapping(value = "/cerca/{registration_number}", produces = "application/json")
public ResponseEntity<UserDto> listUserByRegistrationNumber(@PathVariable("registration_Number") String registrationNumber)
throws ChangeSetPersister.NotFoundException
{
log.info("****** Ottengo l'user con numeroRegistrazione " registrationNumber " *******");
UserDto user = userService.SelByRegistrationNumber(registrationNumber);
if (user == null)
{
String ErrMsg = String.format("L'user con numeroRegistrazione %s non è stato trovato!", registrationNumber);
log.warning(ErrMsg);
throw new ChangeSetPersister.NotFoundException();
}
return new ResponseEntity<UserDto>(user, HttpStatus.OK);
}
}
the dto class
@Data
public class UserDto {
private Long userId;
private LocalDateTime createdDate;
private Long number;
private LocalDateTime modifiedDate;
private String birthPlace;
private Date birthDay;
private Long canNominate;
private String email;
private String firstName;
private String fiscalCode;
private Date hiringDate;
private String lastName;
private String matricola;
private String position;
private String registrationNumber;
private Long replaced;
private Date terminationDate;
private String userStatus;
private Role applicationRoleID;
private Company companyID;
private Direction directions ;
private Levels levelID;
private User referentID;
private Role role;
the repository class
public interface UserRepository extends PagingAndSortingRepository<User, Long> {
@Query(value = "SELECT * FROM USERS WHERE REGISTRATION_NUMBER LIKE :registrationNumber", nativeQuery = true)
List<User> findByRegistrationNumberLike(@Param("registrationNumber") String registrationNumber);
User findByRegistrationNumber(String registrationNumber);
}
the service interface and serviceImpl
public interface UserService {
public Iterable<User> SelTutti();
public List<User> SelByRegistrationNumberLike(String registrationNumber);
public UserDto SelByRegistrationNumber(String registrationNumber);
}
@Service
public class UserServiceImpl implements UserService{
@Autowired
UserRepository userRepository;
@Autowired
ModelMapper modelMapper;
@Override
public Iterable<User> SelTutti() {
return userRepository.findAll();
}
@Override
public List<User> SelByRegistrationNumberLike(String registrationNumber) {
return userRepository.findByRegistrationNumberLike(registrationNumber);
}
@Override
public UserDto SelByRegistrationNumber(String registrationNumber) {
User user = userRepository.findByRegistrationNumber(registrationNumber);
UserDto userDto = null;
if (user != null)
{
userDto = modelMapper.map(user, UserDto.class);
userDto.setMatricola(userDto.getMatricola().trim());
userDto.setUserStatus(userDto.getUserStatus().trim());
}
return userDto;
}
and model map config
@Configuration
public class ModelMapConfig {
@Bean
public ModelMapper modelMapper(){
return new ModelMapper();
}
}
CodePudding user response:
There is a typo in your controller:
//...
@GetMapping(value = "/cerca/{registration_number}", produces = "application/json")
public ResponseEntity<UserDto> listUserByRegistrationNumber(@PathVariable("registration_Number") String registrationNumber)
//...
Note: one time @PathVariable("registration_Number")
is spelled with a capital 'N' and one time with lower case 'n': @GetMapping(value = "/cerca/{registration_number}"
Both must be the same.
CodePudding user response:
Your mapping is:
/cerca/{registration_number}
So you have to call the microservices like this:
http://localhost:8080/cerca/<a valid registration number>