0

Hi I m beginner and I have a simple problem, my url doesn't work. localhost:8080 work but not with /api. The project is built properly.

Localhost:8080

I put @ComponentScan("com.tutojwt.test.repository") because without it doesn't work.

Here my code

Controller :

package com.tutojwt.test.api;

import com.tutojwt.test.model.User;
import com.tutojwt.test.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;

    @GetMapping("/users")
    public ResponseEntity<List<User>>getUsers(){
        return ResponseEntity.ok().body(userService.getUsers());
    }

Testapplication :

package com.tutojwt.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("com.tutojwt.test.repository")
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

}

Project structure

MAJ : Some of you say that is ComponentScan the prob but there are error if I dont put com.tutojwt.test.repository or just com.tutojwt.test

@ComponentScan("com.tutojwt.test") error

Without ComponentScan

ben171
  • 1
  • 1

1 Answers1

0

You should call http://localhost:8080/api/users . @ComponentScan shouldn't be necessary and might actually cause problems (I don't think your controller class is scanned now, and some default hateoas endpoint is the one you're calling). @SpringBootApplication annotation has @EnableAutoConfiguration annotation by default and should scan all component annotated classes. If not, check your class structure again.

BarbetNL
  • 408
  • 2
  • 16