欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

@RequestParam() 和@PathVariable()总是傻傻分不清,记录下

程序员文章站 2022-05-29 11:50:25
...

package com.springdata.jpa01.web;

import com.springdata.jpa01.entity.Man;
import com.springdata.jpa01.repository.ManJpaRepository;
import org.springframework.beans.factory.annotation.Autowired;
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;

import java.util.List;

@RestController
public class ManControllor {

@Autowired
ManJpaRepository repository;

@GetMapping(path = "manid")
public Man getManById(@RequestParam("id") int id) {
    return repository.findManById(id);
}

@GetMapping("manslike/{name}")
public List<Man> getMansByFirstNameLike(@PathVariable("name") String firstName) {
    return repository.findManByFirstNameLike(firstName);
}

}