I've got a problem with @PostMapping
annotation in Spring boot. I deployed application and postgresql database to docker and I'm using docker-compose up to run it. However when I try to send with postman I get error 500. I tried printing data to console and it seems I get data but the id column is null. It has annotation @GeneratedValue
so it should be like that?
name:vardas
Price [appliesFrom=2020, id=null, name=vardas, price=14.0]
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
This is my model file:
package lt.kvk.i15.rupeika_laimonas.models;
import java.sql.Date;
import javax.persistence.*;
import com.fasterxml.jackson.annotation.JsonProperty;
@Entity
@Table(name = "prices")
public class Prices {
@Id @GeneratedValue private Long id;
@Column(name="name")
private String name;
@Column(name="price")
private double price;
@Column(name="applies_from")
private String appliesFrom;
public Prices(){}
public Prices(String name, double price, String appliesFrom) {
this.name = name;
this.price = price;
this.appliesFrom = appliesFrom;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getAppliesFrom() {
return appliesFrom;
}
public void setAppliesFrom(String appliesFrom) {
this.appliesFrom = appliesFrom;
}
@Override
public String toString() {
return "Price [appliesFrom=" appliesFrom ", id=" id ", name=" name ", price=" price "]";
}
}
PriceService.java
package lt.kvk.i15.rupeika_laimonas.services;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import lt.kvk.i15.rupeika_laimonas.models.Prices;
import lt.kvk.i15.rupeika_laimonas.repository.PricesRepository;
@Service
public class PriceService {
private PricesRepository pricesRepository;
public void saveData(@RequestBody Prices price)
{
System.out.println(price.toString());
getPricesRepository().save(price);
}
public void saveAllData(List<Prices> pricesList)
{
getPricesRepository().saveAll(pricesList);
}
public List<Prices> showData()
{
return getPricesRepository().findAll();
}
public PricesRepository getPricesRepository() {
return this.pricesRepository;
}
}
PricesController.java
package lt.kvk.i15.rupeika_laimonas.controllers;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import lt.kvk.i15.rupeika_laimonas.models.Prices;
import lt.kvk.i15.rupeika_laimonas.services.PriceService;
@CrossOrigin(origins="http://localhost:3000")
@RestController
public class PriceController {
private final PriceService priceService;
@Autowired
public PriceController(PriceService priceService)
{
this.priceService = priceService;
}
@RequestMapping("api/prices")
public List<Prices> getPrices() throws FileNotFoundException, IOException
{
return priceService.showData();
}
@PostMapping("api/setoneprice")
public ResponseEntity<Prices> setOnePrice(@RequestBody Prices price)
{
System.out.println("name:" price.getName());
priceService.saveData(price);
return new ResponseEntity<>(price,HttpStatus.CREATED);
}
}
PricesRepository.java
package lt.kvk.i15.rupeika_laimonas.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import lt.kvk.i15.rupeika_laimonas.models.Prices;
@Repository
public interface PricesRepository extends JpaRepository<Prices, Long> {
@Override
List<Prices> findAll();
}
I'm adding all my classes to not miss anything. What could be the problem? My saving is done in saveData()
method.
Edit: Adding full error stack trace:
2021-12-09 21:04:57.865 ERROR 1 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
at lt.kvk.i15.rupeika_laimonas.services.PriceService.saveData(PriceService.java:125) ~[classes!/:0.0.1-SNAPSHOT]
at lt.kvk.i15.rupeika_laimonas.controllers.PriceController.setOnePrice(PriceController.java:68) ~[classes!/:0.0.1-SNAPSHOT]
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:205) ~[spring-web-5.3.10.jar!/:5.3.10]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.10.jar!/:5.3.10]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.10.jar!/:5.3.10]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.10.jar!/:5.3.10]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.10.jar!/:5.3.10]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.10.jar!/:5.3.10]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067) ~[spring-webmvc-5.3.10.jar!/:5.3.10]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) ~[spring-webmvc-5.3.10.jar!/:5.3.10]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.10.jar!/:5.3.10]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.3.10.jar!/:5.3.10]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:681) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.10.jar!/:5.3.10]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.53.jar!/:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.10.jar!/:5.3.10]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.10.jar!/:5.3.10]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.10.jar!/:5.3.10]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.10.jar!/:5.3.10]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.10.jar!/:5.3.10]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.10.jar!/:5.3.10]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1726) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-9.0.53.jar!/:na]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.53.jar!/:na]
at java.base/java.lang.Thread.run(Thread.java:829) ~[na:na]
CodePudding user response:
Your problem was not autowiring the repository, because of that, the dependency was not resolved.
No need for the other things you have done. And please, remove you @RequestBody annotation from your service's method saveData() since this is a service and it has nothing to do with HTTP.
Another important thing is that you should be able to debug your application locally, there is no need of using docker for testing it. If it works without docker, then you start creating your dockerfile and dealing with docker. Problems one by one.
CodePudding user response:
I believe your line "System.out.println(price.toString());" throwing null pointer exception . You trace lines does not match with your code , try to debug the code .
CodePudding user response:
So I actually found the problem. I modified my model class from:
@Id @GeneratedValue private Long id;
@Column(name="name")
private String name;
@Column(name="price")
private double price;
@Column(name="applies_from")
private String appliesFrom;
public Prices(){}
public Prices(String name, double price, String appliesFrom) {
this.name = name;
this.price = price;
this.appliesFrom = appliesFrom;
}
to this:
@Id @GeneratedValue private Long id;
private String name;
private double price;
private String appliesFrom;
public Prices(){}
public Prices(
@JsonProperty("name")String name,
@JsonProperty("price")double price,
@JsonProperty("appliesFrom")String appliesFrom) {
this.name = name;
this.price = price;
this.appliesFrom = appliesFrom;
}
and my services class also. I directly @Autowired
pricesRepository
private PricesRepository pricesRepository;
changed to:
@Autowired
private PricesRepository pricesRepository;
My guess is when I used Postman to send json data it couldn't see it. Hope it helps someone who's having same problem. Thanks for help guys