第一个springboot项目

公司最近的项目在使用springboot和springcloud中的一些组件,刚开始就是主要写一些业务代码,并不了解具体要去怎么配置等,所以最近刚好有时间,就学习学习,记录总结一下,初学,欢迎指正。

开始第一个springboot项目(eclipse+maven):

1. 新建maven项目,pom中添加依赖
<!-- 父项目 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
    <relativePath/>
  </parent>

<!-- 添加依赖 -->
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>
2. 新建启动类
@SpringBootApplication
public class BowlApplication {
    public static void main(String[] args) {
        SpringApplication.run(BowlApplication.class, args);
    }
}
3. 新建类,控制层
@RestController
@RequestMapping(value="/noodles")
@Slf4j
public class BowlController{
    
    /**
     * 首页
     * @param mv
     * @return
     */ 
    @RequestMapping(value="/index")
    public ModelAndView index(ModelAndView mv) {
        log.info(WelcomePage.hello(this.getClass().getSimpleName().toString()));
        mv.addObject("id", "1");
        mv.addObject("name", "bowl");
        FoodInfo food = new FoodInfo();
        food.setRemark1("./sky.jpg");
        food.setRemark2("测试属性1");
        mv.addObject("afood", food);
        mv.setViewName("index");
        return mv;
    }
    
    /**
     * restful风格传参
     * @param mv
     * @param id
     * @return
     */
    @RequestMapping(value="/index/{id}")
    public ModelAndView getFood(ModelAndView mv, @PathVariable("id") Integer id) {
        log.info(WelcomePage.hello(this.getClass().getSimpleName().toString()));
        FoodInfo food = bowlService.findFoodById(id);
        food.setRemark1("../sky.jpg");
        food.setRemark2("测试属性2");
        mv.addObject("afood", food);
        mv.setViewName("index");
        return mv;
    }
}

这个是自己的一个测试,如果只是尝试先运行第一个springboot项目,可以去掉中间传参的部分。

4. 新建配置文件
#启动端口
server:
  port: 55511

#日志
logging:
  file: ./logs/noodles.log

#应用名
spring: 
  application: 
    name: asfood-noodles
#环境
  profiles:
    active: dev

 

5.  新建页面(path: resources/templates/index.html)

页面路径的官方默认有很多,这里就用templates了。页面内容,随便写点吧,主要为了展示出来。

现在启动主类,访问localhost:55511/noodles/index应该就可以访问到index.html页面了。

自己写的练习项目代码在:

https://github.com/molyjao/mlims.git 

原文地址:https://www.cnblogs.com/moly/p/springboot.html