美文网首页
Hello World

Hello World

作者: 城堡下的晚祷 | 来源:发表于2018-08-17 10:11 被阅读0次

    主要实现功能:浏览器发送/hello请求,服务器接受请求并处理,输出hello world字符串。

    1.新建maven项目

    2.pom.xml中加入starter和相关依赖

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.4.RELEASE</version>
            <!--<relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    

    3.编写一个主程序,启动Springboot应用

    helloworlddemo.java

    package com.yzl;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    //来标注一个主程序类,说明这是一个SpringBoot应用
    @SpringBootApplication
    public class helloworlddemo {
        public static void main(String[] args) {
            //spring应用启动起来
            SpringApplication.run(helloworlddemo.class,args);
        }
    }
    

    4.编写Controller

    package com.yzl.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    
    @Controller
    public class HelloController {
        @ResponseBody
        @RequestMapping("/hello")
        public  String hello (){
    
            return "HELLO WORLD";
        }
    }
    

    5.启动主程序

    浏览器通过http://localhost:8080/hello访问,输出:

    HELLO WORLD

    相关文章

      网友评论

          本文标题:Hello World

          本文链接:https://www.haomeiwen.com/subject/hzhzbftx.html