el merrre
el merrre

Reputation: 1

Java Springboot Multiple rest controllers dont work, why?

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

Answers (1)

Ashutosh Dwivedi
Ashutosh Dwivedi

Reputation: 11

I see slash is missing , There is two ways we can define slash in the Request Mapping URL .

  1. Either end class level URL with slash , so in your case @RequestMapping("/api/auth/")

and in the getMapping or PostMapping start without slash so that when the code compile and run , the correct URL path can be generated .

  1. Leave the class level URL as it is without end with slash , so in your case @RequestMapping("/api/auth")

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

Related Questions