【微服务记录】intellij 建立spring boot 项目

博客介绍了Spring项目的新建与代码修改过程。新建项目选择Spring Initializr并选web,代码修改包括新建controller包和类、设置监听端口等。还指出访问出现Whitelabel Error Page的原因是Application启动类位置不对,应放在最外层包含所有子包。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 新建项目

新建项目,选择Spring Initializr

一路默认,然后选择web

2 代码修改

在java目录下新建controller包,在controller包下新建SampleController类

代码为

package hello;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
@EnableAutoConfiguration
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

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

 

修改配置文件,设置监听端口(默认880)

右键运行

在浏览器登录http://127.0.0.1:9090

 

3 Whitelabel Error Page

出现

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Jul 02 11:53:27 CST 2019
There was an unexpected error (type=Not Found, status=404).
No message available

很像是没有读到你要访问的地址。

原因是【Application启动类放的位置不对】要将Application放在最外层,也就是要包含所有子包。

比如你的groupId是com.example.demo,子包就是所谓的com.example.demo.xxx,所以要将Application放在com.example.demo包下。

请参考以下结论:spring-boot会自动加载启动类所在包下及其子包下的所有组件.

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值