spring boot 第一个小项目。

首先在网上找了怎么安装的问题,主要是有两种方式,

1.eclipse->help->eclipse marketplace->popular ->sts插件,我先是用了这种方法,后来发现镜像真的是龟速,阿里云的镜像也没弄明白,所以第一个spring boot没有搭起来。

2.手动@纯洁的微笑,在http://start.spring.io/上直接下载项目,但是现在只支持8和10的版本,跟大佬说的有点出入,于是我一狠心下了最新的eclipse和jdk1.8,之后成功导入了项目文件和相关jar。

在这之后继续按着大佬的安排走,但是在我写完第一个Controller后,并没有达到我想要的结果。原因如下:

Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.

If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
如上的错误是我在选择项目需要的jar包是,手贱包jdbc的导入了进来,加载的时候它就开始找,不过我当然没有配置了,解决如下:
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}) //注解后面的内容是告诉它不要找数据库配置了。
public class DemoApplication {

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

解决完上述的错误后又发生了一个问题,我想要让tomcat用8888的端口,而它默认的是8080,所以我需要修改一下application.properties(默认是空的)加入server.port=8888,这样你启动后的端口号就会显示是8888,

然而在我测试的时候又出现了下面的网页,。。。。。。。。。。。。。


在网上找答案后,发现了问题的所在,项目的结构不对,正确的结构是这样的。

我之前的位置不对,网上的解释说,启动类要放在最外侧,要包含所有子包,最好像下面的结构。

com
  +- example
    +- myproject
      +- Application.java
      |
      +- domain
      |  +- Customer.java
      |  +- CustomerRepository.java
      |
      +- service
      |  +- CustomerService.java
      |
      +- controller
      |  +- CustomerController.java
      |

下面是我controller中的代码。

@RestController
public class Hello {

	@RequestMapping("/hello")
	public String index() {
		return "hello world";
	}
}

这样访问http://localhost:8888/hello,就可以访问成功了。然后你还应该配置热启动,强烈建议。pom.xml

<dependencies>
<dependency>
            <groupId>org.springframework.boot</groupId>  <!--jdbc不解释 -->
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>   <!--java web -->
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

 <dependency>   <!-- 配置热启动的-->

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-devtools</artifactId>

        <optional>true</optional>

   </dependency>

</dependencies>

<build>

    <plugins>

        <plugin>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-maven-plugin</artifactId>

            <configuration>

                <fork>true</fork>

            </configuration>

        </plugin>

   </plugins>

</build>

这样每次你保存文件后,tomcat都会自动重启,而且速度很快,说实话真的太方便了。远比在ssm框架中手动重启更省心。

继续跟着纯洁的微笑大佬学习,还有19个博客的距离。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值