Почему имя проекта появляется в моем запросе Get? ⇐ JAVA
-
Anonymous
Почему имя проекта появляется в моем запросе Get?
У меня есть тестовый код на Java/Spring MVC. Я хочу запустить его и ожидаю, что мой запрос Get будет выглядеть примерно так:
localhost:9090/привет но я вижу
localhost:9090/spring-mvc-app1/hello Я назвал проект Spring-mvc-app1, но не понимаю, почему это имя появляется там, где по логике его быть не должно. Среда разработки – Eclipse.
Сам код:
пакет john.project.controllers; импортировать org.springframework.stereotype.Controller; импортировать org.springframework.web.bind.annotation.GetMapping; @Контроллер общественный класс ControllerOrder { @GetMapping("/привет") публичная строка helloPage() { вернуть «заказ/привет»; } @GetMapping("/до свидания") публичная строка GoodbyePage() { вернуть «заказ/до свидания»; } } Конфигурация: SpringConfig.java:
пакет john.project.config; импортировать org.springframework.beans.factory.annotation.Autowired; импортировать org.springframework.context.ApplicationContext; импортировать org.springframework.context.annotation.Bean; импортировать org.springframework.context.annotation.ComponentScan; импортировать org.springframework.context.annotation.Configuration; импортировать org.springframework.web.servlet.config.annotation.EnableWebMvc; импортировать org.springframework.web.servlet.config.annotation.ViewResolverRegistry; импортировать org.springframework.web.servlet.config.annotation.WebMvcConfigurer; импортировать org.thymeleaf.spring6.SpringTemplateEngine; импортировать org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver; импортировать org.thymeleaf.spring6.view.ThymeleafViewResolver; @Конфигурация @ComponentScan("john.project") @EnableWebMvc публичный класс SpringConfig реализует WebMvcConfigurer { частный окончательный ApplicationContext applicationContext; @Autowired public SpringConfig (ApplicationContext applicationContext) { this.applicationContext = applicationContext; } @Бин public SpringResourceTemplateResolver templateResolver() { SpringResourceTemplateResolver templateResolver = новый SpringResourceTemplateResolver(); templateResolver.setApplicationContext(applicationContext); templateResolver.setPrefix("/WEB-INF/views/"); templateResolver.setSuffix(".html"); вернуть шаблонРезолвер; } @Бин общественный SpringTemplateEngine templateEngine () { SpringTemplateEngine templateEngine = новый SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver()); templateEngine.setEnableSpringELCompiler(истина); вернуть шаблонEngine; } @Override public void configureViewResolvers (реестр ViewResolverRegistry) { Резолвер ThymeleafViewResolver = новый ThymeleafViewResolver(); solver.setTemplateEngine(templateEngine()); реестр.viewResolver(резольвер); } } SpringMvcDispatcherServletInitializer.java:
пакет john.project.config; импортировать org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; публичный класс SpringMvcDispatcherServletInitializer расширяет AbstractAnnotationConfigDispatcherServletInitializer { @Override защищенный класс[] getRootConfigClasses() { вернуть ноль; } @Override защищенный класс[] getServletConfigClasses() { вернуть новый класс[]{SpringConfig.class}; } @Override protected String[] getServletMappings() { вернуть новую строку[]{"/"}; } }
У меня есть тестовый код на Java/Spring MVC. Я хочу запустить его и ожидаю, что мой запрос Get будет выглядеть примерно так:
localhost:9090/привет но я вижу
localhost:9090/spring-mvc-app1/hello Я назвал проект Spring-mvc-app1, но не понимаю, почему это имя появляется там, где по логике его быть не должно. Среда разработки – Eclipse.
Сам код:
пакет john.project.controllers; импортировать org.springframework.stereotype.Controller; импортировать org.springframework.web.bind.annotation.GetMapping; @Контроллер общественный класс ControllerOrder { @GetMapping("/привет") публичная строка helloPage() { вернуть «заказ/привет»; } @GetMapping("/до свидания") публичная строка GoodbyePage() { вернуть «заказ/до свидания»; } } Конфигурация: SpringConfig.java:
пакет john.project.config; импортировать org.springframework.beans.factory.annotation.Autowired; импортировать org.springframework.context.ApplicationContext; импортировать org.springframework.context.annotation.Bean; импортировать org.springframework.context.annotation.ComponentScan; импортировать org.springframework.context.annotation.Configuration; импортировать org.springframework.web.servlet.config.annotation.EnableWebMvc; импортировать org.springframework.web.servlet.config.annotation.ViewResolverRegistry; импортировать org.springframework.web.servlet.config.annotation.WebMvcConfigurer; импортировать org.thymeleaf.spring6.SpringTemplateEngine; импортировать org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver; импортировать org.thymeleaf.spring6.view.ThymeleafViewResolver; @Конфигурация @ComponentScan("john.project") @EnableWebMvc публичный класс SpringConfig реализует WebMvcConfigurer { частный окончательный ApplicationContext applicationContext; @Autowired public SpringConfig (ApplicationContext applicationContext) { this.applicationContext = applicationContext; } @Бин public SpringResourceTemplateResolver templateResolver() { SpringResourceTemplateResolver templateResolver = новый SpringResourceTemplateResolver(); templateResolver.setApplicationContext(applicationContext); templateResolver.setPrefix("/WEB-INF/views/"); templateResolver.setSuffix(".html"); вернуть шаблонРезолвер; } @Бин общественный SpringTemplateEngine templateEngine () { SpringTemplateEngine templateEngine = новый SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver()); templateEngine.setEnableSpringELCompiler(истина); вернуть шаблонEngine; } @Override public void configureViewResolvers (реестр ViewResolverRegistry) { Резолвер ThymeleafViewResolver = новый ThymeleafViewResolver(); solver.setTemplateEngine(templateEngine()); реестр.viewResolver(резольвер); } } SpringMvcDispatcherServletInitializer.java:
пакет john.project.config; импортировать org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; публичный класс SpringMvcDispatcherServletInitializer расширяет AbstractAnnotationConfigDispatcherServletInitializer { @Override защищенный класс[] getRootConfigClasses() { вернуть ноль; } @Override защищенный класс[] getServletConfigClasses() { вернуть новый класс[]{SpringConfig.class}; } @Override protected String[] getServletMappings() { вернуть новую строку[]{"/"}; } }
Мобильная версия