创建一个简单的springboot项目,实现restful接口

本文档指导如何创建一个简单的SpringBoot项目并实现RESTful接口。通过新建project,经过一系列next操作后完成项目创建。初始运行时由于缺少数据库参数配置会遇到问题,但通过添加配置并调整为application.yml(可选),即使有错误配置,SpringBoot也能读取。修复配置后,服务成功启动。接着创建TestController.java,再次启动服务,即可通过http://localhost:8080/test访问接口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

首先new一个project

点击next继续,

next继续

next继续,

点击finish,创建完成

点击运行,出现如下结果:

这是因为缺少数据库的参数配置导致,继续添加数据库参数配置。

application.properties 改为application.yml,可以不改,视个人喜好而定。

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/boot_backend?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
    username: root1
    password: root1
    druid:
      initialSize: 5
      minIdle: 5
      maxActive: 20
      maxWait: 60000
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 300000
      validationQuery: SELECT 1
      testWhileIdle: true
      testOnBorrow: true
      testOnReturn: false
      poolPreparedStatements: true
      maxPoolPreparedStatementPerConnectionSize: 20
      filters: stat,wall
      connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
      stat-view-servlet:
        allow: 127.0.0.1

参数错误没关系,只要有配置,springboot就可以读取到配置,不会报错。

再次启动服务,成功!

创建类TestController.java

package com.liuxy.nes3.controller;

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

@RestController
public class TestController {

    @RequestMapping(value = "/test",method = RequestMethod.GET)
    public String hello() {
        return "test the new spring boot!";
    }
}

再次启动服务,浏览器输入http://localhost:8080/test

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值