一、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!";
}
}
查询后发现spring boot 自定义controller路由找不到,原因是启动类和自定义的Controller包不在同一级目录下
有个大神的帖子http://blog.youkuaiyun.com/Fandly168/article/details/73302281
解决方法:
1.然后把TestController放到和TestApplication同一级目录下后正常访问了
2.在TestApplication里加注解@ComponentScan(basePackages = { "com.spring.controller" })