Java中GET请求与POST请求,前端传参与后端接收实例

此示例以代码方式展现,可直接结合controller层每个接口上方注释与其接口传递参数方式理解!

前端传参直接就以apiPost工具来代替

apiPost调用后端接口几种方式

Java中GET请求与POST请求,前端传参与后端接收实例_第1张图片

 Java中GET请求与POST请求,前端传参与后端接收实例_第2张图片

Java中GET请求与POST请求,前端传参与后端接收实例_第3张图片

Java中GET请求与POST请求,前端传参与后端接收实例_第4张图片

代码:

 controller层:

package com.chensir.ceshi.controller;

import com.chensir.ceshi.entity.User;
import com.chensir.ceshi.service.GetAndPostService;
import com.chensir.handler.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/testGetAndPost")
public class GetAndPostController {

    @Resource
    private GetAndPostService getAndPostService;

    /**
     * 1、可查询全部(什么都不传):http://localhost:8088/testGetAndPost/select1
     * 2、body json方式  值传不过来
     * 3、可 ?拼接(query):http://localhost:8088/testGetAndPost/select1?sex=男
     * 4、body中form-data 值能传过来 可以查询 x-www-form-urlencoded 值传不过来 不能查询
     *
     */
    @GetMapping("/select1")
    public Result selectUser(User user) {
        List userList= getAndPostService.selectUser(user);
        return Result.ok(userList);
    }

    /**
     * 1、查询全部(什么都不传)提示错误请求:http://localhost:8088/testGetAndPost/select2
     * 2、body json方式:可以查询
     * 3、?拼接(query)提示错误请求:http://localhost:8088/testGetAndPost/select1?sex=男
     * 4、body中form-data、x-www-form-urlencoded 提示错误请求
     *
     */
    @GetMapping("/select2")
    public Result selectUser1(@RequestBody User user) {
        List userList= getAndPostService.selectUser(user);
        return Result.ok(userList);
    }

    /**
     * 1、body json方式:值传不过来 查询为空
     * 3、?拼接(query)可以查询:http://localhost:8088/testGetAndPost/select1?name=张三
     * 4、body中form-data:可以查询; x-www-form-urlencoded:值传不过来 查询为空
     *
     */
    @GetMapping("/select3")
    public Result selectUser2(String name) {
        List userList= getAndPostService.selectUserByName(name);
        return Result.ok(userList);
    }

    /**
     * 1、body json方式:提示错误请求
     * 3、?拼接(query)可以查询:http://localhost:8088/testGetAndPost/select1?name=张三
     * 4、body中form-data:可以查询; x-www-form-urlencoded:提示错误请求
     *
     */
    @GetMapping("/select4")
    public Result selectUser3(@RequestParam String name) {
        List userList= getAndPostService.selectUserByName(name);
        return Result.ok(userList);
    }

    /**
     * 1、只能以这样的方式:http://localhost:8088/testGetAndPost/selectUser/张三
     *
     */
    @GetMapping("/selectUser/{name}")
    public Result selectUser4(@PathVariable("name") String name) {
        List userList= getAndPostService.selectUserByName(name);
        return Result.ok(userList);
    }

    /**
     * 和select3一样
     *
     */
    @GetMapping("/select5")
    public Result selectUser5(String name,Integer age) {
        List userList= getAndPostService.selectUserByName1(name,age);
        return Result.ok(userList);
    }

    /**
     * 和select4一样
     *
     */
    @GetMapping("/select6")
    public Result selectUser6(@RequestParam("name") String name,@RequestParam("age") Integer age) {
        List userList= getAndPostService.selectUserByName1(name,age);
        return Result.ok(userList);
    }

    /**
     * 1、只能以这样的方式:http://localhost:8088/testGetAndPost/selectUser/张三/18
     *
     */
    @GetMapping("/selectUser/{name}/{age}")
    public Result selectUser7(@PathVariable("name") String name,@PathVariable("age") Integer age) {
        List userList= getAndPostService.selectUserByName1(name,age);
        return Result.ok(userList);
    }

    /**
     * 1、body json方式:值传不过来
     * 3、?拼接(query)值能传来,能新增:http://localhost:8088/testGetAndPost/select1?name=张三&age=18
     * 4、body中form-data、x-www-form-urlencoded:值都能传来,都可以新增
     *
     */
    @PostMapping("/add1")
    public Result addUser(User user){
        String msg=getAndPostService.addUser(user);
        return Result.ok(msg);
    }

    /**
     * 只能以body json
     *
     */
    @PostMapping("/add2")
    public Result addUser1(@RequestBody User user){
        String msg=getAndPostService.addUser(user);
        return Result.ok(msg);
    }

    /**
     * 1、body json方式:值传不过来
     * 3、?拼接(query)值能传来,能新增:http://localhost:8088/testGetAndPost/select1?name=张三
     * 4、body中form-data、x-www-form-urlencoded:值都能传来,都可以新增
     *
     */
    @PostMapping("/add3")
    public Result addUser2(@RequestParam("name") String name){
        String msg=getAndPostService.addUserName(name);
        return Result.ok(msg);
    }

    /**
     * 只能以 http://localhost:8088/testGetAndPost/selectUser/张三
     *
     */
    @PostMapping("/add4/{name}")
    public Result addUser3(@PathVariable("name") String name){
        String msg=getAndPostService.addUserName(name);
        return Result.ok(msg);
    }
}
 
  

service层:

public interface GetAndPostService {
    List selectUser(User user);

    List selectUserByName(String name);

    List selectUserByName1(String name, Integer age);

    String addUser(User user);

    String addUserName(String name);
}





@Service
public class GetAndPostServiceImpl implements GetAndPostService {

    @Resource
    private GetAndPostMapper getAndPostMapper;


    @Override
    public List selectUser(User user) {
        System.out.println(user.getName());
        return getAndPostMapper.selectUserList(user);
    }

    @Override
    public List selectUserByName(String name) {
        return getAndPostMapper.selectUserByName(name);
    }

    @Override
    public List selectUserByName1(String name, Integer age) {
        return getAndPostMapper.selectUserByName1(name,age);
    }

    @Override
    public String addUser(User user) {
        if (user==null){
            return "user为空";
        }
        int insert = getAndPostMapper.insert(user);
        return insert>0?"ok":"false";
    }

    @Override
    public String addUserName(String name) {
        if (name==null){
            return "name为空";
        }
        User user= User.builder()
                .name(name)
                .build();
        int insert = getAndPostMapper.insert(user);
        return insert>0?"ok":"false";
    }
}

mapper层:

public interface GetAndPostMapper extends BaseMapper {

    List selectUserList(@Param("query") User user);

    List selectUserByName(String name);

    List selectUserByName1(String name, Integer age);
}

xml:





    
    
    

你可能感兴趣的:(java,前端)