0

I have a mysql database with a server table with the following rows:

  • primary id
  • ip address
  • operating system details
  • city location

My controller runs and I'm able to add, update, delete servers. As well as finding all servers.

My goal now is to return the count of servers at a particular location. This is my code on the DAO:

@Override
    public Integer count(Server server) {
        // counts the number of servers at a particular location: only 3 locations are available
        // Palo Alto, Texas, and New Jersey
        String sql = "select count(*) from server_tb s where s.location =: serverLocation";

        Query q = entityManager.createQuery(sql);
        q.setParameter("serverLocation", server.getLocation());
        Integer result = (Integer) q.getSingleResult();
        return result;
    }

This is how the service layer calls DAO:

@Override
    @Transactional
    public Integer count(Server server) {
        // TODO Auto-generated method stub
        return serverDAO.count(server);
    }

And finally, this is how I called service from my controller

@PostMapping("/count")
    public Integer count(@RequestBody Server server) {
        Integer count = serverService.count(server);
        return count;
    }

I get a 400 Bad Request error saying the Request Body is missing, below is the stacktrace I'm getting back. Please help.

> {
>     "timestamp": "2020-06-09T19:26:26.958+00:00",
>     "status": 400,
>     "error": "Bad Request",
>     "trace": "org.springframework.http.converter.HttpMessageNotReadableException:
> Required request body is missing: public java.lang.Integer
> com.project.servers.app.RestController.ServerRestController.count(com.project.servers.app.entity.Server)\r\n\tat
> org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:161)\r\n\tat
> org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:131)\r\n\tat
> org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)\r\n\tat
> org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167)\r\n\tat
> org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)\r\n\tat
> org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)\r\n\tat
> org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879)\r\n\tat
> org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)\r\n\tat
> org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)\r\n\tat
> org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)\r\n\tat
> org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)\r\n\tat
> org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)\r\n\tat
> org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)\r\n\tat
> javax.servlet.http.HttpServlet.service(HttpServlet.java:660)\r\n\tat
> org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)\r\n\tat
> javax.servlet.http.HttpServlet.service(HttpServlet.java:741)\r\n\tat
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)\r\n\tat
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat
> org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)\r\n\tat
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat
> org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)\r\n\tat
> org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\r\n\tat
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat
> org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)\r\n\tat
> org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\r\n\tat
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat
> org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)\r\n\tat
> org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\r\n\tat
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat
> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)\r\n\tat
> org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)\r\n\tat
> org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)\r\n\tat
> org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)\r\n\tat
> org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)\r\n\tat
> org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)\r\n\tat
> org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)\r\n\tat
> org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)\r\n\tat
> org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)\r\n\tat
> org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)\r\n\tat
> org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)\r\n\tat
> org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)\r\n\tat
> java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)\r\n\tat
> java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)\r\n\tat
> org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\r\n\tat
> java.base/java.lang.Thread.run(Thread.java:834)\r\n",
>     "message": "Required request body is missing: public java.lang.Integer
> com.project.servers.app.RestController.ServerRestController.count(com.project.servers.app.entity.Server)",
>     "path": "/main/count" }
owtu16
  • 11
  • 1
  • 2
  • You haven't provide the 2 most importants elements to find the problem : the HTTP request used in your test and your Server class – Fabien Jun 09 '20 at 20:04
  • (1) You're looking for a query parameter, not a request body. (2) This should generally be a GET, not a POST. (3) You're reinventing Spring Data _and_ not using generics. Use Spring Data JPA and watch the magic. – chrylis -cautiouslyoptimistic- Jun 09 '20 at 20:37
  • Please provide Server class detail and sample input which you are using. – Yogesh Prajapati Jun 10 '20 at 06:56

1 Answers1

3

Since your endpoint is a HTTP POST method and you are expecting a request body, your request should include a valid JSON in the body. You have not posted the Server class here, but from the fields I am guessing it is something like this :

{ 
  "id" : 1,
  "ipAddress" : "xx.xx..xx",
  "osDetails" : "details",      
  "location" : "some location" 
 }

The JSON should represent the fields of the server class.

Not a part of the question, but in my opinion, you could use a GET endpoint here with @RequestParam. POST is to create resources.

Eklavya
  • 17,618
  • 4
  • 28
  • 57