使用STS搭建一个简单的服务

版权声明:@CopyRight转载请注明出处 https://blog.csdn.net/LI_AINY/article/details/87265812
  1. file->new Spring Starter Project
    在这里插入图片描述
  2. 简单搭建一个 只选择web
    在这里插入图片描述
    点击finish,然后开始已下载组件了,可以去抽根烟等一等
    。。。
    也可以使用intellijIDEA搭建springboot项目https://blog.csdn.net/LI_AINY/article/details/86227506,intellijIDEA对Spring也是完美支持的
  3. 新建成功后
    目录结构
    在这里插入图片描述
    gradle文件夹、gradlewgradlew.bat 是用来在没有安装gradle环境时方便构建程序用的,因为已经安装了,所以这几个文件可以删除。
    安装方法https://blog.csdn.net/LI_AINY/article/details/87254280
    build.gradle 文件是用来管理工程依赖的,有点像以前用Maven管理项目中的pom.xml文件。
    src/main/resources 用来存放静态资源、配置信息、页面模板文件等。
    application.properties 是核心配置文件,可以用来配置服务端口、数据源信息等等, STS提供了友好的提示功能,在录入的时候能自动补全配置信息。
  4. 增加服务端口
    在application.properties中添加:server.port=8801
    启动后就可以通过 http://localhost:8801 来访问服务了
  5. 增加一个提供RESTfull查询接口类
package com.comment.user;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
//此类为controller类
@RestController
public class GetName {
	//请求路径
	 @GetMapping("/user/getName")
	    String getUser() {
	        return "ljw";
	    }
}

  1. 打开启动类Application,右键Run As->Spring Boot App
    在这里插入图片描述
  2. 访问地址http://127.0.0.1:8801/user/getName
    结果:
    在这里插入图片描述
    OJBK

猜你喜欢

转载自blog.csdn.net/LI_AINY/article/details/87265812