https://start.spring.io/  

SpringBoot实战 | 第一篇:Spring Boot搭建和启动

2

获得rest-spring-mvc.zip 

解压后,IDEA导入该项目。

在com.wsc.restspringmvc下创建pojo,然后创建Person类

SpringBoot实战 | 第一篇:Spring Boot搭建和启动

package com.wsc.restspringmvc.pojo;

public class Person {
   private long id;
   private String name;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

3

在com.wsc.restspringmvc下创建controller,然后创建PersonRestController

SpringBoot实战 | 第一篇:Spring Boot搭建和启动

package com.wsc.restspringmvc.controller;

import com.wsc.restspringmvc.pojo.Person;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PersonRestController {

    @GetMapping("/person/{id}")
    public Person person(@PathVariable Long id, @RequestParam(required = false) String name){
        Person per=new Person();
        per.setId(id);
        per.setName(name);
        return per;
    }
}

4 运行RestSpringMvcApplication.java

浏览器输入:

http://localhost:8080/person/1?name=袅袅炊烟

结果:

{"id":1,"name":"袅袅炊烟"}

 5

注意:

1.IDEA中Alt+4可切换到console

相关文章:

  • 2021-06-08
  • 2021-04-25
  • 2021-05-25
  • 2021-07-04
  • 2021-10-23
  • 2021-04-19
  • 2022-01-09
  • 2021-08-02
猜你喜欢
  • 2021-05-17
  • 2022-01-20
  • 2021-10-12
  • 2021-07-04
  • 2022-12-23
  • 2022-01-19
  • 2021-04-27
相关资源
相似解决方案