The first springboot project

The company's recent projects use some components in springboot and springcloud. At first, they mainly write some business code, do not know how to configure it, etc. So recently, just have time to learn, record and summarize, and welcome to make corrections.

Start the first springboot project (eclipse+maven):

1. New maven project, add dependencies to pom
<!-- Parent Project -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
    <relativePath/>
  </parent>

<!-- Add Dependency -->
<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. New Startup Class
@SpringBootApplication
public class BowlApplication {
    public static void main(String[] args) {
        SpringApplication.run(BowlApplication.class, args);
    }
}
3. New Class, Control Layer
@RestController
@RequestMapping(value="/noodles")
@Slf4j
public class BowlController{
    
    /**
     * home page
     * @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("Test Attribute 1");
        mv.addObject("afood", food);
        mv.setViewName("index");
        return mv;
    }
    
    /**
     * restful Style Passage
     * @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("Test Attribute 2");
        mv.addObject("afood", food);
        mv.setViewName("index");
        return mv;
    }
}

This is a test of your own, and if you just try to run the first springboot project first, you can remove the middle argument.

4. New Profile
#Start Port
server:
  port: 55511

#Journal
logging:
  file: ./logs/noodles.log

#Application Name
spring: 
  application: 
    name: asfood-noodles
#Environmental Science
  profiles:
    active: dev

 

5. New page (path: resources/templates/index.html)

There are many official page paths by default, so templates are used here.Page content, write a few, mainly for display.

Now start the main class, accessLocalhost:55511/noodles/indexShould be accessibleIndex.htmlPage.

 

Write your own exercise item code in:

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

Tags: Java Spring SpringBoot Maven Attribute

Posted on Fri, 17 Jul 2020 11:28:51 -0400 by businessman332211