Eclipse中构建一个Spring Boot项目

一 Spring Boot的依赖从哪里获取
二 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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>org.crazyi.cloud</groupId>
      <artifactId>first-boot</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      
      <dependencies>
            <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-web</artifactId>
                  <version>1.5.7.RELEASE</version>
            </dependency>
      </dependencies>
      
</project>
三 编写启动类
package org.crazyit.cloud;

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

@SpringBootApplication
public class FirstApp {

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

}
四 编写一个Controller
package org.crazyit.cloud;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MyController {

    @GetMapping("/hello")
    @ResponseBody
    public String hello() {
        return "Hello World";
    }
}
五 启动并测试
六 新建实体类
package org.crazyit.cloud;
public class Person {
      private Integer id;
      private String name;
      private Integer age;
      public Integer getId() {
            return id;
      }
      public void setId(Integer id) {
            this.id = id;
      }
      public String getName() {
            return name;
      }
      public void setName(String name) {
            this.name = name;
      }
      public Integer getAge() {
            return age;
      }
      public void setAge(Integer age) {
            this.age = age;
      }
      
}
七 新建REST风格的控制类
package org.crazyit.cloud;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyRestController {

    @RequestMapping(value = "/person/{id}", method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public Person getPerson(@PathVariable Integer id) {
        Person p = new Person();
        p.setId(id);
        p.setName("angus");
        p.setAge(30);
        return p;
    }
}
八 测试


猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/81041200