1. 程式人生 > >springboot(1)搭建第一步

springboot(1)搭建第一步

一、eclipse搭建個springboot的專案

1.jdk用了1.8

2.maven用了3.5,資源庫用的阿里的http://maven.aliyun.com/nexus/content/groups/public/

3.eclipse 新建maven專案

4.pom.xml如下

<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 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.leng.springboot</groupId>
	<artifactId>springboot1.x</artifactId>
	<packaging>war</packaging>
	<version>1.0.0-SNAPSHOT</version>
	<name>springboot</name>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.1.RELEASE</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>1.1.7</version>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-core</artifactId>
		</dependency>
</dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

中間標紅的是由於啟動springboot過程中報錯了ch.qos.logback.core.joran.spi.JoranException,發現少了倆logback的jar包,於是又添加了這倆包

5.測試啟動類

/**
 * lengchen
 */
package com.spring.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author lengchen
 *
 */
@SpringBootApplication
public class TestApplication {

	/**
	 * 測試
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
	}
}

6.測試controller類

/**
 * lengchen
 */
package com.spring.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 
 * @author lengchen
 * @date 2018年1月3日
 */
@RestController
public class TestController {

    /**
     * 列印
     * @return
     */
    @RequestMapping(value="/hello",method={RequestMethod.POST,RequestMethod.GET},produces = "application/json; charset=UTF-8")
    public String print(){
        return "Spring boot!";
    }
}
啟動後訪問http://127.0.0.1:8080/hello頁面404

查詢後發現spring boot 自定義controller路由找不到,原因是啟動類和自定義的Controller包不在同一級目錄下

有個大神的帖子http://blog.csdn.net/Fandly168/article/details/73302281

解決方法:

1.然後把TestController放到和TestApplication同一級目錄下後正常訪問了

2.在TestApplication里加註解@ComponentScan(basePackages = { "com.spring.controller" })