springboot第一天 快速启动,json输出

来源:互联网 发布:阿里云ecs快照服务 编辑:程序博客网 时间:2024/05/21 15:04

自动生成springboot的maven项目 网址:http://start.spring.io/


一.springboot 快速启动:

1.新建一个maven web项目

2.配置pom文件,加入springboot的代码:

pom文件的配置:

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>spring-boot</groupId>
    <artifactId>springboot01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot01</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.0.RELEASE</version>
    </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>
            </plugin>
        </plugins>
    </build>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>


</project>

3.新建测试类:
启动文件:

package spring_boot.springboot01;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@SpringBootApplication
@Controller
public class TestController {
    
      @RequestMapping("/")
      @ResponseBody
      public String test(){
          System.out.println("====================");
        return "你好您好Hello Wordsfdd !fgdfgfhgfhgf";
    }

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

最后一步:

 右击项目 点击 run As   ==> Maven clear    ====> maven install   ===> 运行main 函数 ===》 在浏览器输入:localhost:8080/


二.springboot返回json数据

1.新建maven项目

2.编写pom文件:

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>spring-boot</groupId>
    <artifactId>springboot02</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot02</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

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


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>

            <groupId>com.alibaba</groupId>

            <artifactId>fastjson</artifactId>

            <version>1.2.20</version>

        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3.创建实体类:

public class User {
    
    private int id;
    private String name;
    private String address;
    private String age;

  get和set 还有toString方法这里省略..

}

4.创建测试controller

1).第一种方式:

package spring_boot.springboot02.controller;

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

import spring_boot.springboot02.model.User;

@RestController
public class TestController {
    @RequestMapping("/")
    public User test() {
        User user = new User();
        user.setId(12);
        user.setName("张三丰");
        user.setAddress("四川成都");
        user.setAge("140岁");
        return user;
    }
}

运行项目:

右击项目 点击 run As   ==> Maven clear    ====> maven install  

 右击项目 点击 run As ===> java application ===》 在浏览器输入:localhost:8080/


页面访问:
2).第二种方式:

package spring_boot.springboot02.controller;

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

import spring_boot.springboot02.model.User;

@Controller
public class TestController2 {
    @ResponseBody
    @RequestMapping("/test2")
    public User test() {
        User user = new User();
        user.setId(13);
        user.setName("张四丰");
        user.setAddress("四川成都");
        user.setAge("150岁");
        return user;
    }
}

运行项目:

右击项目 点击 run As   ==> Maven clear    ====> maven install  

 右击项目 点击 run As ===> java application ===》 在浏览器输入:localhost:8080/ test2








原创粉丝点击