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

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

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

  1. public class SupMain {
  2. private Supplier supplier;
  3. private Supplier_Part supplierPart;
  4. public SupMain(Supplier supplier, Supplier_Part supplierPart) {
  5. this.supplier = supplier;
  6. this.supplierPart = supplierPart;
  7. }
  8. public Supplier getSupplier() {
  9. return supplier;
  10. }
  11. public Supplier_Part getSupplierPart() {
  12. return supplierPart;
  13. }
  14. }

Supplier.java

  1. @Entity
  2. @Table
  3. public class Supplier {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.AUTO)
  6. private Long sup_Id;
  7. private String sup_name;
  8. private String sup_mailId;
  9. private int sup_phoneNo;
  10. private String sup_location;
  11. private String sup_feedback;
  12. public Supplier_Part getSupplier_part() {
  13. return supplier_part;
  14. }
  15. public void setSupplier_part(Supplier_Part supplier_part) {
  16. this.supplier_part = supplier_part;
  17. }
  18. @ManyToOne(fetch = FetchType.LAZY)
  19. @JoinColumn(name = "part_Id",nullable=false,unique=true)
  20. private Supplier_Part supplier_part;
  21. public Supplier() { }
  22. public Supplier(Long sup_Id, String sup_name, String sup_mailId, int sup_phoneNo, String sup_location, String sup_feedback) {
  23. this.sup_Id = sup_Id;
  24. this.sup_name = sup_name;
  25. this.sup_mailId = sup_mailId;
  26. this.sup_phoneNo = sup_phoneNo;
  27. this.sup_location = sup_location;
  28. this.sup_feedback = sup_feedback;
  29. }
  30. // Rest of the methods...
  31. }

Similarly, I have another Supplier_Part entity also.

SupplierController.java

  1. @Controller
  2. public class SuppliersController {
  3. @Autowired
  4. SupplierService supplierService;
  5. @Autowired
  6. Supplier_PartRepoistory supplier_partRepoistory;
  7. @Autowired
  8. SupplierRepository supplierRepository;
  9. @RequestMapping("/suppliers/{id}")
  10. public List<Supplier> getAllSuppliers(@Param("id") Long id){
  11. return supplierService.getSupliersByPart(id);
  12. }
  13. @RequestMapping("/addSuppliers", method = RequestMethod.POST)
  14. public Supplier addSupplier(@RequestBody SupMain supMain){
  15. Supplier supplier = supMain.getSupplier();
  16. System.out.println(supplier);
  17. Supplier_Part supplierPart = supMain.getSupplierPart();
  18. List<Supplier> list = new ArrayList<>();
  19. list.add(supplier);
  20. supplierPart.setSupplierList(list);
  21. supplier.setSupplier_part(supplierPart);
  22. supplierRepository.save(supplier);
  23. supplier_partRepoistory.save(supplierPart);
  24. return supplier;
  25. }
  26. }

This is how my JSON data looks like:

  1. {
  2. "Supplier": {
  3. "sup_Id": 1,
  4. "sup_name": "Arpita",
  5. "sup_mailId": "arpitamitra",
  6. "sup_phoneNo": "98856565",
  7. "sup_location": "bakers street",
  8. "sup_feedback": "Best"
  9. },
  10. "Supplier_Part": {
  11. "part_Id": 1,
  12. "part_name": "Monitor",
  13. "part_desc": "xyzzz",
  14. "part_quantity": 20,
  15. "part_timePeriod": 5
  16. }
  17. }

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

  1. java.lang.NullPointerException: null
  2. at com.cognizant.Suppliers.Controller.SuppliersController.addSupplier(SuppliersController.java:46) ~[classes/:na]
  3. // 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

  1. private Supplier supplier;
  2. private Supplier_Part supplierPart;
  3. public SupMain(Supplier supplier, Supplier_Part supplierPart) {
  4. this.supplier = supplier;
  5. this.supplierPart = supplierPart;
  6. }
  7. public Supplier getSupplier() {
  8. return supplier;
  9. }
  10. public Supplier_Part getSupplierPart() {
  11. return supplierPart;
  12. }
  13. }

Supplier.java

  1. @Table
  2. public class Supplier {
  3. @Id
  4. @GeneratedValue(strategy = GenerationType.AUTO)
  5. private Long sup_Id;
  6. private String sup_name;
  7. private String sup_mailId;
  8. private int sup_phoneNo;
  9. private String sup_location;
  10. private String sup_feedback;
  11. public Supplier_Part getSupplier_part() {
  12. return supplier_part;
  13. }
  14. public void setSupplier_part(Supplier_Part supplier_part) {
  15. this.supplier_part = supplier_part;
  16. }
  17. @ManyToOne(fetch = FetchType.LAZY)
  18. @JoinColumn(name = &quot;part_Id&quot;,nullable=false,unique=true)
  19. private Supplier_Part supplier_part;
  20. public Supplier() { }
  21. public Supplier(Long sup_Id, String sup_name, String sup_mailId, int sup_phoneNo, String sup_location, String sup_feedback) {
  22. this.sup_Id = sup_Id;
  23. this.sup_name = sup_name;
  24. this.sup_mailId = sup_mailId;
  25. this.sup_phoneNo = sup_phoneNo;
  26. this.sup_location = sup_location;
  27. this.sup_feedback = sup_feedback;
  28. }
  29. public Long getSup_Id() {
  30. return sup_Id;
  31. }
  32. public void setSup_Id(Long sup_Id) {
  33. this.sup_Id = sup_Id;
  34. }
  35. public String getSup_name() {
  36. return sup_name;
  37. }
  38. public void setSup_name(String sup_name) {
  39. this.sup_name = sup_name;
  40. }
  41. public String getSup_mailId() {
  42. return sup_mailId;
  43. }
  44. public void setSup_mailId(String sup_mailId) {
  45. this.sup_mailId = sup_mailId;
  46. }
  47. public int getSup_phoneNo() {
  48. return sup_phoneNo;
  49. }
  50. public void setSup_phoneNo(int sup_phoneNo) {
  51. this.sup_phoneNo = sup_phoneNo;
  52. }
  53. public String getSup_location() {
  54. return sup_location;
  55. }
  56. public void setSup_location(String sup_location) {
  57. this.sup_location = sup_location;
  58. }
  59. public String getSup_feedback() {
  60. return sup_feedback;
  61. }
  62. public void setSup_feedback(String sup_feedback) {
  63. this.sup_feedback = sup_feedback;
  64. }
  65. @Override
  66. public String toString() {
  67. return &quot;Supplier{&quot; +
  68. &quot;sup_Id=&quot; + sup_Id +
  69. &quot;, sup_name=&#39;&quot; + sup_name + &#39;\&#39;&#39; +
  70. &quot;, sup_mailId=&#39;&quot; + sup_mailId + &#39;\&#39;&#39; +
  71. &quot;, sup_phoneNo=&quot; + sup_phoneNo +
  72. &quot;, sup_location=&#39;&quot; + sup_location + &#39;\&#39;&#39; +
  73. &quot;, sup_feedback=&#39;&quot; + sup_feedback + &#39;\&#39;&#39; +
  74. &#39;}&#39;;
  75. }
  76. }

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

  1. @Controller
  2. public class SuppliersController {
  3. @Autowired
  4. SupplierService supplierService;
  5. @Autowired
  6. Supplier_PartRepoistory supplier_partRepoistory;
  7. @Autowired
  8. SupplierRepository supplierRepository;
  9. @RequestMapping(&quot;/suppliers/{id}&quot;)
  10. public List&lt;Supplier&gt; getAllSuppliers(@Param(&quot;id&quot;) Long id){
  11. return supplierService.getSupliersByPart(id);
  12. }
  13. @RequestMapping(&quot;/addSuppliers&quot;,method = RequestMethod.POST)
  14. public Supplier addSupplier(@RequestBody SupMain supMain){
  15. Supplier supplier=supMain.getSupplier();
  16. System.out.println(supplier);
  17. Supplier_Part supplierPart=supMain.getSupplierPart();
  18. List&lt;Supplier&gt; list=new ArrayList&lt;&gt;();
  19. list.add(supplier);
  20. supplierPart.setSupplierList(list);
  21. supplier.setSupplier_part(supplierPart);
  22. supplierRepository.save(supplier);
  23. supplier_partRepoistory.save(supplierPart);
  24. return supplier;
  25. }
  26. }

This is how my JSON data looks like

  1. {
  2. &quot;Supplier&quot;:{
  3. &quot;sup_Id&quot;:1,
  4. &quot;sup_name&quot;:&quot;Arpita&quot;,
  5. &quot;sup_mailId&quot;:&quot;arpitamitra&quot;,
  6. &quot;sup_phoneNo&quot;:&quot;98856565&quot;,
  7. &quot;sup_location&quot;:&quot;bakers street&quot;,
  8. &quot;sup_feedback&quot;:&quot;Best&quot;
  9. },
  10. &quot;Supplier_Part&quot;:{
  11. &quot;part_Id&quot;:1,
  12. &quot;part_name&quot;:&quot;Monitor&quot;,
  13. &quot;part_desc&quot;:&quot;xyzzz&quot;,
  14. &quot;part_quantity&quot;:20,
  15. &quot;part_timePeriod&quot;:5
  16. }
  17. }

Getting a 500 internal server error with

  1. java.lang.NullPointerException: null
  2. at com.cognizant.Suppliers.Controller.SuppliersController.addSupplier(SuppliersController.java:46) ~[classes/:na]
  3. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
  4. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
  5. at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
  6. at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
  7. at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  8. at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  9. at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  10. at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  11. at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  12. at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  13. at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  14. at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  15. at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  16. at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  17. at javax.servlet.http.HttpServlet.service(HttpServlet.java:652) ~[tomcat-embed-core-9.0.37.jar:4.0.FR]
  18. at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  19. at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.37.jar:4.0.FR]
  20. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  21. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  22. at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.37.jar:9.0.37]
  23. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  24. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  25. at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  26. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  27. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  28. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  29. at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  30. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  31. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  32. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  33. at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  34. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  35. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  36. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  37. at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  38. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  39. at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  40. at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  41. at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  42. at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  43. at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  44. at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  45. at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  46. at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  47. at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  48. at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  49. at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
  50. at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
  51. at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  52. 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"
},

  1. "supplierPart": {
  2. "part_Id": 1,
  3. "part_name": "Monitor",
  4. "part_desc": "xyzzz",
  5. "part_quantity": 20,
  6. "part_timePeriod": 5
  7. }

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

Thanks!

英文:

The JSON data needs to be updated !

  1. {
  2. &quot;supplier&quot;:{
  3. &quot;sup_Id&quot;:1,
  4. &quot;sup_name&quot;:&quot;Arpita&quot;,
  5. &quot;sup_mailId&quot;:&quot;arpitamitra&quot;,
  6. &quot;sup_phoneNo&quot;:&quot;98856565&quot;,
  7. &quot;sup_location&quot;:&quot;bakers street&quot;,
  8. &quot;sup_feedback&quot;:&quot;Best&quot;
  9. },
  10. &quot;supplierPart&quot;:{
  11. &quot;part_Id&quot;:1,
  12. &quot;part_name&quot;:&quot;Monitor&quot;,
  13. &quot;part_desc&quot;:&quot;xyzzz&quot;,
  14. &quot;part_quantity&quot;:20,
  15. &quot;part_timePeriod&quot;:5
  16. }
  17. }

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:

确定