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

springboot 2.1.6通用controller中@RequestBody无法获取到参数

程序员文章站 2022-03-20 08:23:17
...

一直想写一个通用Controller,最近试了试,使用的是面向接口编程。

BaseController如下:

package com.scgc.es.controller;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;

import java.io.Serializable;

/**
 * @author Dongping Yin
 */

public interface BaseController<T, ID extends Serializable> {
    public Object get();

    public Object get(@PathVariable ID id);

    public Object insert(@ModelAttribute T t);

    public Object update(@ModelAttribute T t);

    public Object delete(@PathVariable ID id);
}

BaseControllerImpl如下:

package com.scgc.es.controller.v1;

import com.scgc.es.controller.BaseController;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @description: TODO
 * @author: Yin DongPing
 * @create: 2020/01/02
 */
@NoArgsConstructor
public class BaseControllerImpl<T, ID extends Serializable> implements BaseController<T, ID> {
    @Override
    public Object get() {
        return null;
    }

    @Override
    public Object get(ID id) {
        return null;
    }

    @Override
    public Object insert(T t) {
        return null;
    }

    @Override
    public Object update(T t) {
        return null;
    }

    @Override
    public Object delete(ID id) {
        return null;
    }
}

调用类InfoController如下:

package com.scgc.es.controller.v1;

import com.scgc.es.annotations.ResponseResult;
import com.scgc.es.controller.BaseController;
import com.scgc.es.model.po.Info;
import com.scgc.es.service.es.InfoEsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @description: ES 索引 API
 * @author: Yin DongPing
 * @create: 2020/01/02
 */
@RestController
@RequestMapping("/api/v1/infos")
@Slf4j
@ResponseResult
public class InfoController extends BaseControllerImpl<Info, Integer> implements BaseController<Info, Integer> {
    private final InfoEsService infoEsService;

    public InfoController(InfoEsService infoEsService) {
        this.infoEsService = infoEsService;
    }


    @PostMapping
    public Object insert(@RequestBody Info info) {
        return infoEsService.save(info);
    }
}

前端用POSTMAN传递JSON请求,结果后端一直接收不到参数。

springboot 2.1.6通用controller中@RequestBody无法获取到参数

springboot 2.1.6通用controller中@RequestBody无法获取到参数

Debug之后发现只要InfoController去掉了implements BaseController<Info, Integer>这段代码就可以接受到参数了。

具体原因没有查明,后面有时间了再说。