postman 在springboot中获取null values对象

db2dz4w8  于 12个月前  发布在  Postman
关注(0)|答案(1)|浏览(152)

我有一个springboot应用程序,当我从postman发布JSON时,我得到了null值。

Springboot文件:

`package com.learnspringboot.demo.controller;

import com.learnspringboot.demo.entity.Department;
import com.learnspringboot.demo.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @PostMapping("/departments")
    public Department saveDepartment(@RequestBody Department department){
       ** System.out.println(department.toString());**
        return departmentService.saveDepartment(department);
    }

    @GetMapping("/departments")
    public List<Department> fetchDepartment(){
        return departmentService.fetchDepartment();
    }

    @DeleteMapping("/departments/{Id}")
    public String deleteDepartment(@PathVariable("Id") Long DepartmentId){
        departmentService.deleteDepartment(DepartmentId);
        return "Department Id: "+ DepartmentId +" Deleted Successfully";
    }
}
`

字符串
Springboot console logs
Postman Request
任何解决此问题的建议将受到高度赞赏。
我试着用实际的IP替换它,但它不起作用。

1tuwyuhd

1tuwyuhd1#

您在Postman中发送的JSON对象需要与您Map到的Java对象匹配。在您的情况下,它将是Department对象。看起来您正在发送以开头的字段,这导致了问题。
试着在 Postman 上发送下面的内容(注意字段是 Camel 大小写)

{
    "departmentName": "India",
    "departmentCode": "TT",
    "departmentAddress": "001"
}

字符串

相关问题