zoukankan      html  css  js  c++  java
  • SpringBoot入门笔记(一)、HelloWorld

    本文是一篇SprintBoot学习入门笔记

    1、打开Eclipse,版本为Oxygen 4.7.0

    2、新建项目
    NewProject->MavenProject->Next->Next

    GroupId填写com.learn,Artifactid 填写spring-boot-hello,完成

    3、配置pom.xml

    双击pom.xml,打开pom.xml选项卡,因为暂时不需要Test,删除Test有关的,另外删除项目Test目录。
    修改后的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/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.kfit</groupId>
      <artifactId>spring-boot-hello</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>spring-boot-hello</name>
      <url>http://maven.apache.org</url>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- 指定jdk1.8 -->
        <java.version>1.8</java.version>
      </properties>
    
      <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
      </dependencies>
    </project>

    4、新建Controller类

    内容如下

    package com.learn.spring_boot_hello;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    @RestController
    public class HelloController {
    
        @RequestMapping("/hello")
        public String hello() {
            return "hello";
        }
    }

    5、App.Java中启动

    package com.learn.spring_boot_hello;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * 在这里使用@SpringBootApplication指定这是一个sprint boot的应用程序
     *
     */
    @SpringBootApplication
    public class App 
    {
        public static void main( String[] args )
        {
            SpringApplication.run(App.class, args);
        }
    }

    6、项目右键Run,选择App,Ok

    7、查看Console,如果输出以下内容表示运行正常

    8、浏览器测试
    http://127.0.0.1:8080/hello
    输出
    hello

    9、新建一个Student实体类

    package com.learn.spring_boot_hello;
    
    public class Student {
    
        int id;
        String name;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        
        
    }

    10、Controller里编写对应的方法

        @RequestMapping("/getstudent")
        public Student getStudent() {
            Student student=new Student();
            student.id=1;
            student.name="小明";
            return student;
        }

    11、启动
    http://127.0.0.1:8080/getstudent

    {"id":1,"name":"小明"}

    从上面可以看出会自动把对象转为json,默认使用的jackson解析框架。

    (完毕)



  • 相关阅读:
    Microsoft SQL Server 2008 基本安装说明
    微软的招聘哲学——做微软人的五大核心素质
    大型网站性能优化的通用方法
    模型驱动的开发,回忆一年多前的一次开发
    远离客户陷阱小故事 转
    单例模式(Singleton)
    真的很高兴,就在今天“博客园团队”为我们开通了 “CSLA 团队”
    桥接模式(Bridge)与合成/聚合复用原则(CARP)
    2009年11月11日
    虚拟化的好处 随笔
  • 原文地址:https://www.cnblogs.com/zhaogaojian/p/8975962.html
Copyright © 2011-2022 走看看