P
P
parkito2017-04-12 01:13:42
Java
parkito, 2017-04-12 01:13:42

Why can endpoints be launched from only one rest controller?

Hello. Please help me solve the following problem.
I am writing a RESTFul application in spring.
This is how I set up the web config

public class AppInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext container) throws ServletException {

        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(AppConfig.class);
        ctx.setServletContext(container);

        ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));

        servlet.setLoadOnStartup(1);
        servlet.addMapping("/");
    }
}

Now I create two rest controllers
@RestController
public class MainRestController {
    static final Logger log = LogManager.getLogger(MainRestController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private GroupService groupService;

    @Autowired
    private RoomService roomService;

    @Autowired
    private UserEntityToUserDTO userEntityToUserDTO;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String homePage() {
        return "index";
    }

.....

and
@RestController
public class UserController {
    private static Logger logger = LogManager.getLogger(UserController.class);

    @Autowired
    UserService userService;

    @Autowired
    UserEntityToUserDTO userToUserDTO;

    @Autowired
    UserDTOtoUserEntity userDTOtoUser;

    @Autowired
    GroupService groupService;

    @RequestMapping(value = "/getUserByCredentials", method = RequestMethod.GET)
    public ResponseEntity<UserDTO> getUserByCredentials(HttpServletRequest req,
                                                        @RequestParam(value = "email") String email,
                                                        @RequestParam(value = "password") String password) {

....

The problem is that I have access to the addresses that are mapped in the MainRestController , everything that is in the UserController is not visible (404). I don't understand how the controllers are different. And why one works and the other is ignored.
UPD
@Configuration
@Import({JpaConfig.class,
        BaseControlRestConfig.class,
        AppInitializer.class})
@ComponentScan(basePackages = "com.roommate.basecontrol")
@EnableWebMvc
public class AppConfig  {


}

@Configuration
@ComponentScan(value = "com.roommate.basecontrol.controllers.restControllers")
@Import({BaseControlServiceConfig.class,
        BaseControlModelConfig.class})
public class BaseControlRestConfig {
    @Bean
    public RequestContextListener requestContextListener() {
        return new RequestContextListener();
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question