【java】1000问3SpringBoot空项目如何创建最简易API

1、首先确认搭建的demo没有报错,当前我使用的是jar方式。

在package中创建controller包,并创建HelloController

package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloController {
 	@RequestMapping("/index")
 	public String index(String id){
 		return id;
 	}
}

之后直接运行DemoApplication:

浏览器访问:http://localhost:8080/hello/index?id=123123 说明运行成功

如何更换端口号,比如说我配置了https,端口号为43,那么在配置文件application.properties 中进行配置:

在resources中加入application.properties文件,里面加入 最好不要用空格。

server.port=8081

 

一般来说springBoot不使用properties来进行配置,而是使用.yml格式的文件来配置属性文件如下:后期建议采集这种方式。注意内容修改完毕之后,将文件后缀改为.yml

server:
  port: 8081

 

之后启动项目报错:

Caused by: org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token
found character '\t(TAB)' that cannot start any token. (Do not use \t(TAB) for indentation)
 in 'reader', line 2, column 1:
    	port:8081
    ^
 	at org.yaml.snakeyaml.scanner.ScannerImpl.fetchMoreTokens(ScannerImpl.java:419)

解决方式:删除port前的tab改为两个个空格的缩进方式。

 

重新启动项目报错:

***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class

此时改为yml默认是去加载一些数据库配置信息的,但是没有找到,所以报错,在启动类上添加注解:

package com.example.demo;

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class DemoApplication {

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

}

重新启动项目:(后面如果配置了yml的数据库信息的话,就可以将这个注解删除掉了。见第四问的配置)

启动成功:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值