创建一个简单SpringBootWeb项目

创建一个简单SpringBootWeb项目,第1张

创建一个简单SpringBootWeb项目

首先创建一个maven项目

        打开idea,创建maven项目,并修改pom文件



    4.0.0

    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.0.RELEASE 
    
    org.example
    SpringBootTest
    1.0-SNAPSHOT

    
        8
        8
    

    

        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.projectlombok
            lombok
        

    

 创建SpringBoot启动

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

@SpringBootApplication //启动类注解,表示这个是个SpringBoot项目
public class RunApplication {
    public static void main(String[] args) {
        SpringApplication.run(RunApplication.class, args);//启动方法
    }
}

创建controller包,并创建测试的controller类,controller包必须在启动类(RunApplication.java)同级或者下级,因为SpringBoot默认会扫描启动类同级或者之下的包

 实体类代码

import lombok.Data;

@Data
public class UserVO {
    private String name;
    private String sex;
    private Integer age;
}

 TestController代码

import com.springboot.entity.UserVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController {

    @ResponseBody
    @RequestMapping("/test")
    public String test(){
        return "接口测试成功";
    }

    @ResponseBody
    @RequestMapping("/getUser")
    public UserVO getUser(){
        UserVO userVO = new UserVO();
        userVO.setName("张三");
        userVO.setAge(18);
        userVO.setSex("男");
        return userVO;
    }
}

在resources目录下新建application.yml文件并配置服务地址

server:
  port: 8081 #服务地址

启动SpringBoot

在页面访问http://localhost:8081/test ,接口正常返回数据

 访问地址http://localhost:8081/getUser ,接口正常返回对象的json数据

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/5671401.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存