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的数据库信息的话,就可以将这个注解删除掉了。见第四问的配置)
启动成功: