• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

에러:

21.02.09 13:09 작성 조회수 166

0

제목과 같은 This application has no explicit mapping for /error, so you are seeing this as a fallback. 에러가 발생합니다ㅜㅜ

아래는 소스 코드 입니다.

[수정 전]

1. HelloWorldBean. java

package com.example.restfulwebservice;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data //setter + getter
@NoArgsConstructor
public class HelloWorldBean {
    private String message;
        public HelloWorldBean(String message){
        this.message = message;
    }
}

2. HelloWorldController

package com.example.restfulwebservice;

import com.example.restfulwebservice.HelloWorldBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Locale;

@RestController
class HelloWorldController {
    @Autowired
    private MessageSource messageSource;
    // GET
    // /hello-world (endpoint)
    // @RequestMapping(method=RequestMethod.GET, path="/hello-world")
    @GetMapping(path = "/hello-world")
    public String helloWorld() {
        return "Hello World";
    }

    // alt + enter
    @GetMapping(path = "/hello-world-bean")
    public HelloWorldBean helloWorldBean() {
        return new HelloWorldBean("Hello World");
    }

}

<실행 결과>

[수정 후]

https://hororolol.tistory.com/368

위 링크를 참고해 HelloWorldBean.java에 @ComponentScan를 추가했더니, error는 발생하지 않습니다.

- HelloWorldBean.java

package com.example.restfulwebservice;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.context.annotation.ComponentScan;

@Data //setter + getter
@NoArgsConstructor
@ComponentScan(basePackages = {"com.example.restfulwebservice"})
public class HelloWorldBean {
    private String message;
        public HelloWorldBean(String message){
        this.message = message;
    }
}

<실행 결과>

[번외]

HelloWorldBean.java 파일에서 강의와 같이 @AllArgsConstructor, @NoArgsConstructor 어노테이션을 붙여줬을 때 아래와 같은 에러가 납니다 ㅜㅜ

package com.example.restfulwebservice;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data //setter + getter
@AllArgsConstructor
@NoArgsConstructor
public class HelloWorldBean {
    private String message;
}

version을 맞추기 위해 아래와 같이 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.13.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>restful-web-service</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>restful-web-service</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<version>2.1.13.RELEASE</version>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

답변 2

·

답변을 작성해보세요.

0

서진규(Jake Seo)님의 프로필

서진규(Jake Seo)

2021.09.13

혼자서 문제해결을 해보시다가 완전히 잘못된 길로 빠지신 거 같은데,...

 

프로젝트 다시 만드시고 똑같이 해보시면 에러 안날 겁니다.

 

@ComponentScan 애노테이션은 HelloWorldBean에 작성하셔야 될 내용이 아니라, 

메인 스프링부트 애플리케이션.java 파일에 자동으로 붙어있는 @SpringBootApplication 애노테이션이 포함하는 애노테이션 중 하나입니다.

고로 강의와 같이 스프링부트 스타터로 시작하셨다면 웬만해선 수동으로 추가하실 필요가 없습니다.

@Autowired MessageSource messageSource;  이 부분도 빼주세요. 해당 부분은 스프링부트에서 사용하는 기본 MessageSource 빈을 가져올 때 사용하는 소스입니다. 현재 학습 내용과 전혀 관련이 없습니다.

이 강의에서 소스코드를 추가하는 부분은 매우 적으니 다시 처음부터 빨리감기로 보시면서 소스코드 변화부분만 그대로 따라하시면 에러 안 나실 겁니다.

0

혜멍님의 프로필

혜멍

질문자

2021.02.09

helloworldbean.java -> HelloWorldBean class에서 Sysout으로 찍어보면 Hello World라고 잘 뜨는데... ㅠㅠ 웹에는 찍히지 않는것이 의문입니다..ㅠㅠ

System.out.print(message);