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

huangapple go评论138阅读模式
英文:

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

问题

I have used Wrapper Class to keep both the Objects in JSON but when I am trying to fetch it using @RequestBody in my Controller I am getting a NullPointerException. I think the code structure will make more sense in understanding.

Model - SupMain.java

public class SupMain {

    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

@Entity
@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;
    }

    // Rest of the methods...
}

Similarly, I have another Supplier_Part entity also.

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;
    }
}

This is how my JSON data looks like:

{
    "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
    }
}

Getting a 500 internal server error with the following stack trace:

java.lang.NullPointerException: null
    at com.cognizant.Suppliers.Controller.SuppliersController.addSupplier(SuppliersController.java:46) ~[classes/:na]
    // Rest of the stack trace...

Thanks a Ton in advance!

英文:

I have used Wrapper Class to keep both the Objects in JSON but when I am trying to fetch it using @RequestBody in my Controller I am getting a NullPointerException. I think the code structure will make more sense in understanding.

Model - 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 = &quot;part_Id&quot;,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 &quot;Supplier{&quot; +
                &quot;sup_Id=&quot; + sup_Id +
                &quot;, sup_name=&#39;&quot; + sup_name + &#39;\&#39;&#39; +
                &quot;, sup_mailId=&#39;&quot; + sup_mailId + &#39;\&#39;&#39; +
                &quot;, sup_phoneNo=&quot; + sup_phoneNo +
                &quot;, sup_location=&#39;&quot; + sup_location + &#39;\&#39;&#39; +
                &quot;, sup_feedback=&#39;&quot; + sup_feedback + &#39;\&#39;&#39; +

                &#39;}&#39;;
    }
}

Similarly I have another Supplier_Part entity also.
I have two repositories that are extending CRUD and a Controller where I am trying to keep a rest end point of POST where I want to take two objects from JSON and store them to my DB.

SupplierController.java


@Controller
public class SuppliersController {

    @Autowired
    SupplierService supplierService;

    @Autowired
    Supplier_PartRepoistory supplier_partRepoistory;

    @Autowired
    SupplierRepository supplierRepository;

    @RequestMapping(&quot;/suppliers/{id}&quot;)
    public List&lt;Supplier&gt; getAllSuppliers(@Param(&quot;id&quot;) Long id){
       return supplierService.getSupliersByPart(id);
    }

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

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

        List&lt;Supplier&gt; list=new ArrayList&lt;&gt;();
        list.add(supplier);


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

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

        return supplier;



    }

}

This is how my JSON data looks like

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

Getting a 500 internal server error with

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]

Thanks a Ton in advance !

答案1

得分: 0

"Supplier" 和 "Supplier_Part" 是输入的键,它们不允许数据绑定到您的 SupMain 类属性 "supplier" 和 "supplier_part"。它们应该与类属性的大小写相同。重命名它们并尝试,我相信它应该能工作。

英文:

It's the keys "Supplier" and "Supplier_Part" of the input which is not letting the data gets bound to your SupMain class property "supplier" and supplier_part".
It should be of the same case as the class property.
Rename it and try, I believe it should work.

答案2

得分: 0

{
"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
}

}
instead of Supplier and Supplier_Part!
SupMain class has two private variables name supplier and supplierPart

Thanks!

英文:

The JSON data needs to be updated !

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

instead of Supplier and Supplier_Part !
SupMain class has two private variables name supplier and supplierPart

Thanks !

huangapple
  • 本文由 发表于 2020年8月3日 12:29:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63223762.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定