Reputation: 1
Im trying to write 2 rest controllers in separate files.
here is one controller:
@RestController
@RequestMapping("/api/auth")
public class AuthController {
@PostMapping("login")
public ResponseEntity<JwtAuthResponeDto> login(@RequestBody LoginDto loginDto){
Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(
loginDto.getUsername(),
loginDto.getPassword()));
SecurityContextHolder.getContext().setAuthentication(authentication);
String token = jwtTokenProvider.generateToken(authentication);
return new ResponseEntity<>(new JwtAuthResponeDto(token), HttpStatus.OK);
}
@PostMapping("register")
public ResponseEntity<String> register(@RequestBody RegisterDto registerDto) {
if (userRepository.existsByUsername(registerDto.getUsername())) {
return new ResponseEntity<>("Username is taken!", HttpStatus.BAD_REQUEST);
}
UserEntity user = new UserEntity();
user.setUsername(registerDto.getUsername());
user.setPassword(passwordEncoder.encode((registerDto.getPassword())));
RoleEntity role = roleRepository.findByName("role_user").get();
user.setRoles(Collections.singletonList(role));
userRepository.save(user);
return new ResponseEntity<>("User registered success!", HttpStatus.OK);
}}
here is second:
@RestController
@RequestMapping("/views")
public class ViewController {
@GetMapping("login")
public ModelAndView login(){
return new ModelAndView("login");
}
}
The View Controller dont show the view, Why? But when i put this GetMapping to first controller then it works fine, Why?
App is running and when i put /views/login webbrowser dont hsow anything and i get on terminal: 2023-06-12 13:20:59.994 INFO 10516 --- [nio-8081-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2023-06-12 13:20:59.995 INFO 10516 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2023-06-12 13:21:00.003 INFO 10516 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 3 ms
Upvotes: 0
Views: 228
Reputation: 11
I see slash is missing , There is two ways we can define slash in the Request Mapping URL .
and in the getMapping or PostMapping start without slash so that when the code compile and run , the correct URL path can be generated .
and in the getMapping or PostMapping start with slash so that when the code compile and run , the correct URL path can be generated.
In the current code there is a issue which can be corrected by accepting either point 1 or point 2 .
Thanks , Ashutosh
Upvotes: 0