快速构建Spring boot项目

1、Idea里新建项目

2、创建HelloController

3、运行

4、开发环境热部署

pom.xml

查看目前已有的依赖

配置properties

设置

ctrl+shift+alt+/

新版本的compiler.automake.allow.when.app.running已经不在registry里面了,在settings里面的Advanced settings里面Allow auto-make to start even if developed application is currently running 就可以了。记得确认!!!

5、控制器

package com.example.helloworld.controller;

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

/**
 * @Author Fxdll
 * @Date 2024/4/22 22:24
 * @PackageName:com.example.helloworld.controller
 * @ClassName: HelloController
 * @Description: TODO
 * @Version 1.0
 */
@RestController
public class HelloController {
    //www.example.com/hello
    //https://localhost:8080/hello
    //   @RequestMapping(value = "/hello",method = RequestMethod.GET)
    //    @GetMapping("/hello")

    //https://localhost:8080/hello?nickname=zhangsan

    @RequestMapping( "/hello")
    //@ResponseBody
    public String hello(){
    return "你好世界";
    }
}

6、参数传递

整体代码

package com.example.helloworld.controller;

import com.example.helloworld.demos.web.User;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Fxdll
 * @Date 2024/4/28 23:20
 * @PackageName:com.example.helloworld.controller
 * @ClassName: ParamsController
 * @Description: TODO
 * @Version 1.0
 */

@RestController
public class ParamsController {

  //http://localhost:8080/getTest1
  //路由getTest1    方法名:getTest1
//  @GetMapping("/getTest1/{id}")
//  public String getTest1(@PathVariable("id") int id) {
//    return "GET请求";
//  }
  //http://localhost:8080/getTest1
  @RequestMapping(value = "/getTest1", method = RequestMethod.GET)
  public String getTest1() {
    return "GET请求";
  }

  @RequestMapping(value = "/getTest2", method = RequestMethod.GET)
  //http://localhost:8080/getTest2?nickname=fxdll&phone=123456
    public String getTest2(String nickname,String phone) {
    System.out.println("nickname:"+nickname);
    System.out.println("phone:"+phone);
    return "GET请求";
  }
//加上RequestParam 可以把nickname和name绑定在一起,加上requestparam则表示必须要传递nickname参数,否则会报错,加上required=false则表示nickname参数可以不传递
  @RequestMapping(value = "/getTest3", method = RequestMethod.GET)
  public String getTest3(@RequestParam(value = "nickname",required=false ) String name) {
    System.out.println("nickname:"+name);
    return "GET请求: ";
  }

  @RequestMapping(value = "/getTest4", method = RequestMethod.GET)
  public String getTest4( String name) {
    System.out.println("nickname  :"+name);
    return "GET请求: ";
  }

  //http://localhost:8080/postTest1
  //路由postTest1    方法名:postTest1
  @RequestMapping(value = "/postTest1", method = RequestMethod.POST)
  public String postTest1() {
    return "POST请求: ";
  }
  //http://localhost:8080/postTest2?username=fxdll&password=123456
  //路由postTest2    方法名:postTest2
  @RequestMapping(value = "/postTest2", method = RequestMethod.POST)
  public String postTest2(String username, String password) {
    System.out.println("username:"+username);
    System.out.println("password:"+password);
    return "post请求 ";
  }


  @RequestMapping(value = "/postTest3", method = RequestMethod.POST)
    public String postTest3(User user) {
    System.out.println(user);
    return "POST请求 ";
  }

  @RequestMapping(value = "/postTest4", method = RequestMethod.POST)
    public String post4(@RequestBody User user) {
    System.out.println(user);
    return "post请求 ";
  }

  @GetMapping("/test/**")
  public String get() {
    return "通配符参数: ";
  }
}

package com.example.helloworld.controller;

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

/**
 * @Author Fxdll
 * @Date 2024/4/22 22:24
 * @PackageName:com.example.helloworld.controller
 * @ClassName: HelloController
 * @Description: TODO
 * @Version 1.0
 */
@RestController
public class HelloController {
       @RequestMapping(value = "/hello",method = RequestMethod.GET)
    //https://localhost:8080/hello?nickname=zhangsan
//    @RequestMapping( "/hello")
    public String hello(String nickname){
        return "你好" + nickname;
    }
}

请求

POST2

多个参数时

package com.example.helloworld.controller;


import com.example.helloworld.entity.User;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Fxdll
 * @Date 2024/4/28 23:20
 * @PackageName:com.example.helloworld.controller
 * @ClassName: ParamsController
 * @Description: TODO
 * @Version 1.0
 */

@RestController
public class ParamsController {

  //http://localhost:8080/getTest1
  //路由getTest1    方法名:getTest1
//  @GetMapping("/getTest1/{id}")
//  public String getTest1(@PathVariable("id") int id) {
//    return "GET请求";
//  }
  //http://localhost:8080/getTest1
  @RequestMapping(value = "/getTest1", method = RequestMethod.GET)
  public String getTest1() {
    return "GET请求";
  }

  @RequestMapping(value = "/getTest2", method = RequestMethod.GET)
  //http://localhost:8080/getTest2?nickname=fxdll&phone=123456
    public String getTest2(String nickname,String phone) {
    System.out.println("nickname:"+nickname);
    System.out.println("phone:"+phone);
    return "GET请求";
  }
//加上RequestParam 可以把nickname和name绑定在一起,加上requestparam则表示必须要传递nickname参数,否则会报错,加上required=false则表示nickname参数可以不传递
  @RequestMapping(value = "/getTest3", method = RequestMethod.GET)
  public String getTest3(@RequestParam(value = "nickname",required=false ) String name) {
    System.out.println("nickname:"+name);
    return "GET请求: ";
  }

  @RequestMapping(value = "/getTest4", method = RequestMethod.GET)
  public String getTest4( String name) {
    System.out.println("nickname  :"+name);
    return "GET请求: ";
  }

  //http://localhost:8080/postTest1
  //路由postTest1    方法名:postTest1
  @RequestMapping(value = "/postTest1", method = RequestMethod.POST)
  public String postTest1() {
    return "POST请求: ";
  }
  //http://localhost:8080/postTest2?username=fxdll&password=123456
  //路由postTest2    方法名:postTest2
  @RequestMapping(value = "/postTest2", method = RequestMethod.POST)
  public String postTest2(String username, String password) {
    System.out.println("username:"+username);
    System.out.println("password:"+password);
    return "post请求 ";
  }


  @RequestMapping(value = "/postTest3", method = RequestMethod.POST)
    public String postTest3(User user) {
    System.out.println(user);
    return "POST请求 ";
  }
  //当需要接受json格式数据时,需要加上@RequestBody注解
  @RequestMapping(value = "/postTest4", method = RequestMethod.POST)
    public String post4(@RequestBody User user) {
    System.out.println(user);
    return "post请求 ";
  }

  @GetMapping("/test/**")
  public String get() {
    return "通配符参数: ";
  }
}

user实体类

get set方法

package com.example.helloworld.entity;

/**
 * @Author Fxdll
 * @Date 2024/4/29 23:33
 * @PackageName:com.example.helloworld.entity
 * @ClassName: User
 * @Description: TODO
 * @Version 1.0
 */
public class User {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }


    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }



    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", passwordl='" + password + '\'' +
                '}';        // 省略其他属性
    }
}

post4

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值