SpringBoot:How 我可以使用 Wrapper Class 将两个 JSON pojo 绑定到两个单独的对象中并将它们保存到我的数据库中吗?

SpringBoot:How can I bind two JSON pojo into two seperate objects using Wrapper Class and save them to my DB?

我已经使用 Wrapper Class 将两个对象都保存在 JSON 中,但是当我尝试在我的控制器中使用 @RequestBody 获取它时,我得到了 NullPointerException。我认为代码结构在理解上会更有意义。

型号 - SupMain.java


    private Supplier supplier;
    private Supplier_Part supplierPart;

    public SupMain(Supplier supplier, Supplier_Part supplierPart) {
        this.supplier = supplier;
        this.supplierPart = supplierPart;
    }

    public Supplier getSupplier() {
        return supplier;
    }

    public Supplier_Part getSupplierPart() {
        return supplierPart;
    }


}

Supplier.java

@Table
public class Supplier {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long sup_Id;
    private String sup_name;
    private String sup_mailId;
    private int sup_phoneNo;
    private String sup_location;
    private String sup_feedback;

    public Supplier_Part getSupplier_part() {
        return supplier_part;
    }

    public void setSupplier_part(Supplier_Part supplier_part) {
        this.supplier_part = supplier_part;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "part_Id",nullable=false,unique=true)
     private Supplier_Part supplier_part;


    public Supplier() { }

    public Supplier(Long sup_Id, String sup_name, String sup_mailId, int sup_phoneNo, String sup_location, String sup_feedback) {
        this.sup_Id = sup_Id;
        this.sup_name = sup_name;
        this.sup_mailId = sup_mailId;
        this.sup_phoneNo = sup_phoneNo;
        this.sup_location = sup_location;
        this.sup_feedback = sup_feedback;

    }


    public Long getSup_Id() {
        return sup_Id;
    }

    public void setSup_Id(Long sup_Id) {
        this.sup_Id = sup_Id;
    }

    public String getSup_name() {
        return sup_name;
    }

    public void setSup_name(String sup_name) {
        this.sup_name = sup_name;
    }

    public String getSup_mailId() {
        return sup_mailId;
    }

    public void setSup_mailId(String sup_mailId) {
        this.sup_mailId = sup_mailId;
    }

    public int getSup_phoneNo() {
        return sup_phoneNo;
    }

    public void setSup_phoneNo(int sup_phoneNo) {
        this.sup_phoneNo = sup_phoneNo;
    }

    public String getSup_location() {
        return sup_location;
    }

    public void setSup_location(String sup_location) {
        this.sup_location = sup_location;
    }

    public String getSup_feedback() {
        return sup_feedback;
    }

    public void setSup_feedback(String sup_feedback) {
        this.sup_feedback = sup_feedback;
    }





    @Override
    public String toString() {
        return "Supplier{" +
                "sup_Id=" + sup_Id +
                ", sup_name='" + sup_name + '\'' +
                ", sup_mailId='" + sup_mailId + '\'' +
                ", sup_phoneNo=" + sup_phoneNo +
                ", sup_location='" + sup_location + '\'' +
                ", sup_feedback='" + sup_feedback + '\'' +

                '}';
    }
}

同样,我还有另一个 Supplier_Part 实体。 我有两个扩展 CRUD 的存储库和一个控制器,我试图在其中保留 POST 的休息端点,我想从 JSON 中取出两个对象并将它们存储到我的数据库中。

SupplierController.java


@Controller
public class SuppliersController {

    @Autowired
    SupplierService supplierService;

    @Autowired
    Supplier_PartRepoistory supplier_partRepoistory;

    @Autowired
    SupplierRepository supplierRepository;

    @RequestMapping("/suppliers/{id}")
    public List<Supplier> getAllSuppliers(@Param("id") Long id){
       return supplierService.getSupliersByPart(id);
    }

    @RequestMapping("/addSuppliers",method = RequestMethod.POST)
    public Supplier addSupplier(@RequestBody SupMain supMain){

        Supplier supplier=supMain.getSupplier();
        System.out.println(supplier);
        Supplier_Part supplierPart=supMain.getSupplierPart();

        List<Supplier> list=new ArrayList<>();
        list.add(supplier);


        supplierPart.setSupplierList(list);
        supplier.setSupplier_part(supplierPart);

        supplierRepository.save(supplier);
        supplier_partRepoistory.save(supplierPart);

        return supplier;



    }

}

这就是我的 JSON 数据的样子

{
    "Supplier":{
        "sup_Id":1,
        "sup_name":"Arpita",
        "sup_mailId":"arpitamitra",
        "sup_phoneNo":"98856565",
        "sup_location":"bakers street",
        "sup_feedback":"Best"
    },
    
    "Supplier_Part":{
        "part_Id":1,
        "part_name":"Monitor",
        "part_desc":"xyzzz",
        "part_quantity":20,
        "part_timePeriod":5
    }
}

出现 500 内部服务器错误
java.lang.NullPointerException: null
    at com.cognizant.Suppliers.Controller.SuppliersController.addSupplier(SuppliersController.java:46) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:652) ~[tomcat-embed-core-9.0.37.jar:4.0.FR]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.37.jar:4.0.FR]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.37.jar:9.0.37]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]

提前致谢!

输入的键“供应商”和“Supplier_Part”不允许数据绑定到您的 SupMain class 属性“供应商”和 supplier_part”。 它应该与 class 属性 的大小写相同。 重命名试试,我相信应该可以。

JSON数据需要更新!

{
    "supplier":{
        "sup_Id":1,
        "sup_name":"Arpita",
        "sup_mailId":"arpitamitra",
        "sup_phoneNo":"98856565",
        "sup_location":"bakers street",
        "sup_feedback":"Best"
    },
    
    "supplierPart":{
        "part_Id":1,
        "part_name":"Monitor",
        "part_desc":"xyzzz",
        "part_quantity":20,
        "part_timePeriod":5
    }
}

而不是供应商和Supplier_Part! SupMain class 有两个私有变量名称 supplier 和 supplierPart

谢谢!