0

I am working with a javascript client that I am trying to use to communicate with a server. I have a Javascript function that is POSTing to a Spring Boot REST service. The service is a simple test service that doesn't do much...

@RequestMapping(method = RequestMethod.POST,value="/testservice")
@ResponseBody
public String testPostRequest(@RequestParam String someText)
{
   System.out.println("Reached the counting service! Param value: " + someText);

   if(someText != null)
   {
      ...
      perform some actions
      ...
   }

   return("Success");
}

The Javascript I am using to send POST requests to the server is below:

var sendWords = function(toSend) {
    var data = { DataList : [toSend] };
    var param = { someText: data };

    $.post("http://localhost:8080/testservice",param,
        function(status,ret) {
          alert("We're back "+status);
    });
};

The toSend parameter is just a string containing some text that will be posted to the service. Note that the port for the service was set to 8080 in the server's application.properties file.

When I call the Javascript function and post the string to the service, I get the following log message from the server:

2019-07-28 20:00:06.292  WARN 80844 --- [nio-8080-exec-8] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'someText' is not present]

I am doing something wrong, but cannot figure out what it is. I am sending an object that is tagged as the someText string, but it is not being recognized by the server for some reason.

Can someone tell me what I am missing? How do I get this to work?

Factor Three
  • 2,094
  • 5
  • 35
  • 51

2 Answers2

1

var data = { DataList : [toSend] }; var param = { someText: data }; Here data is not string. You may need to stringify it.

var param = { someText: JSON.stringify(data) };

Shovan
  • 185
  • 7
0

You are missing the value in the @RequestParam(), such as @RequestParam(value="someText", required=true).

You could use @PathVariable too. This could be helpful: @RequestParam vs @PathVariable