SpringBoot의 요청 매핑이 작동하지 않음
저는 애플리케이션 빌드를 위한 스프링 부트를 배우고 있습니다.다른 패키지의 컨트롤러를 응용 프로그램으로 사용하여 첫 번째 Spring Boot 응용 프로그램을 구축하려고 합니다.Tomcat 인스턴스가 표시되지만 요청이 URI에 등록된 RestController에 도달하지 않습니다.
다음은 컨트롤러 클래스입니다.
package com.spring.controllers;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping(value = "/abc")
public String getHi() {
System.out.println("End Point hit");
return "Hi";
}
}
다음은 응용 프로그램 클래스입니다.
package com.spring.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = {"com.spring.controllers"})
public class SpringBootMain {
public static void main(String args[]) {
SpringApplication.run(SpringBootMain.class, args);
}
}
Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>RandomProjects</groupId>
<artifactId>SpringBoot</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>com.spring.boot.SpringBootMain</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.6.RELEASE</version>
</dependency>
</dependencies>
Tomcat 시작 시간 기록:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.6.RELEASE)
2017-12-25 16:41:16.236 INFO 50314 --- [BootMain.main()] com.spring.boot.SpringBootMain : Starting SpringBootMain on f45c89be9049.ant.amazon.com with PID 50314 (/Users/sumt/Desktop/Work/RandomWork/RandomProjects/target/classes started by sumt in /Users/sumt/Desktop/Work/RandomWork/RandomProjects)
2017-12-25 16:41:16.244 INFO 50314 --- [BootMain.main()] com.spring.boot.SpringBootMain : No active profile set, falling back to default profiles: default
2017-12-25 16:41:16.408 INFO 50314 --- [BootMain.main()] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7bbcce75: startup date [Mon Dec 25 16:41:16 IST 2017]; root of context hierarchy
2017-12-25 16:41:18.279 INFO 50314 --- [BootMain.main()] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-12-25 16:41:18.295 INFO 50314 --- [BootMain.main()] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2017-12-25 16:41:18.297 INFO 50314 --- [BootMain.main()] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.16
2017-12-25 16:41:18.402 INFO 50314 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-12-25 16:41:18.402 INFO 50314 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2056 ms
2017-12-25 16:41:18.560 INFO 50314 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-12-25 16:41:18.564 INFO 50314 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-12-25 16:41:18.565 INFO 50314 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-12-25 16:41:18.565 INFO 50314 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-12-25 16:41:18.566 INFO 50314 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-12-25 16:41:18.920 INFO 50314 --- [BootMain.main()] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7bbcce75: startup date [Mon Dec 25 16:41:16 IST 2017]; root of context hierarchy
2017-12-25 16:41:19.005 INFO 50314 --- [BootMain.main()] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-12-25 16:41:19.007 INFO 50314 --- [BootMain.main()] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-12-25 16:41:19.041 INFO 50314 --- [BootMain.main()] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-25 16:41:19.041 INFO 50314 --- [BootMain.main()] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-25 16:41:19.084 INFO 50314 --- [BootMain.main()] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-25 16:41:19.323 INFO 50314 --- [BootMain.main()] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-12-25 16:41:19.407 INFO 50314 --- [BootMain.main()] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-12-25 16:41:19.413 INFO 50314 --- [BootMain.main()] com.spring.boot.SpringBootMain : Started SpringBootMain in 3.889 seconds (JVM running for 6.196)
기본 패키지 스캔을 추가하고 시도했습니다.@ComponentScan
주석, 그러나 URL(http://localhost:8080/abc)을 눌러도 결과는 동일합니다.
예기치 않은 오류(type=Not Found, status=404)가 발생했습니다.사용 가능한 메시지 없음
같은 문제에 직면한 다른 사람이 있습니까?어떻게 해결하셨는지 제안 부탁드립니다.
컨트롤러는 SpringBootApplication의 하위 패키지에 있어야 합니다.예를 들어 SpringbootApplication 메인 메서드가 com.text.demo 패키지 아래에 있는 경우 컨트롤러 클래스는 com.text.demo.controller 아래에 있어야 합니다.
또는 추가@ComponentScan("com.text.demo.controller") on Spring boot Application main class.
댓글에 언급된 대로 기본 패키지를 추가하려고 합니다.
@SpringBootApplication(scanBasePackages = {"com.spring"})
또는
@SpringBootApplication
@ComponentScan("com.spring")
하지만 이 spring initializr https://start.spring.io/ 을 사용하여 처음부터 시작하는 Spring boot 프로젝트를 만드는 것은 어떨까요?필요한 종속성을 추가할 수도 있습니다.
다음은 사용자의 예제에서 재현하려고 했던 미니 컨트롤러입니다.
스프링 어플리
package com.example.demo.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.example.demo.controller")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
컨트롤러
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping(value = "/abc")
public String getHi() {
System.out.println("End Point hit");
return "Hi";
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
그리고 그것은 잘 작동합니다!
예를 들어 기본 작동기 끝점을 호출해 보십시오.http://localhost:8080/error
Spring Boot가 제공되고 서비스의 실제 상태가 표시됩니다.
상위 버전 번호를 아래로 변경합니다.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
이 문제는 @Service 주석을 잘못 가져왔기 때문일 수 있습니다.그것은 정형화된 것이어야 합니다.
Springboot에서 구성 요소 스캔을 사용하는 것이 더 나은 방법이 아니므로 서비스 클래스에서 이 방법을 사용하십시오.org.springframework.springpe 가져오기.서비스;
언급URL : https://stackoverflow.com/questions/47967898/requestmapping-of-springboot-not-working
'code' 카테고리의 다른 글
숨겨진 셀 찾기 수행 (0) | 2023.07.02 |
---|---|
gitsvn을 사용하여 원격 지점 체크아웃 (0) | 2023.07.02 |
Log4같은 로거에 대해 두 개의 다른 수준에서 두 개의 다른 추가기에 대한 Net 로깅 (0) | 2023.07.02 |
Mongoid / Mongodb 및 내장된 문서 쿼리 (0) | 2023.07.02 |
데이터베이스 동의어는 언제 사용해야 합니까? (0) | 2023.07.02 |