简体   繁体   中英

spring boot not seeing request parameters

My angular app is calling a REST API web service run by Spring boot. Here is the parameter dump from the firefox web console:

在此处输入图片说明

My spring boot webapp is throwing this exception:

org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'payload' is not present
    at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:198) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:109) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:158) [spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128) [spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) [javax.servlet-api-3.1.0.jar:3.1.0]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) [javax.servlet-api-3.1.0.jar:3.1.0]
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:841) [jetty-servlet-9.4.7.v20170914.jar:9.4.7.v20170914]
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1650) [jetty-servlet-9.4.7.v20170914.jar:9.4.7.v20170914]
    at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:206) [websocket-server-9.4.7.v20170914.jar:9.4.7.v20170914]

And here is my method...

@RequestMapping(value = "/login", method = RequestMethod.POST)
@ResponseBody
public String login(@RequestParam(NetworkKeyNames.KEY_PAYLOAD) String payload) throws IOException {
    log.error("Payload=" + payload);
    TypeReference<Map<String, String>> typeRef = new TypeReference<Map<String, String>>() {
    };
    Map<String, String> payloadMap = objectMapper.readValue(payload, typeRef);
    String username = payloadMap.get(NetworkKeyNames.KEY_USERNAME);
    String password = payloadMap.get(NetworkKeyNames.KEY_PASSWORD);

    String result = securityService.login(username, password);
    return result;
}

It's obvious that it's hitting my spring boot app, but what's happening to the parameters? Is there any way I can get more information?

Try this, I'm not sure of your version of Spring Boot, you may have to tweak it a bit.

    // Somewhere in package /service/dto
class PayloadDTO {
 String payload;

 public setPayload(String payload) {
  this.payload = payload;
 }

 public getPayload() {
  return payload;
 }
}

// Resource method
@PostMapping("/login")
public String login(@RequestBody PayloadDTO payload) throws IOException {
 log.error("Payload=" + payload.getPayload());
 TypeReference < Map < String, String >> typeRef = new TypeReference < Map < String, String >> () {};
 Map < String, String > payloadMap = objectMapper.readValue(payload.getPayload(), typeRef);
 String username = payloadMap.get(NetworkKeyNames.KEY_USERNAME);
 String password = payloadMap.get(NetworkKeyNames.KEY_PASSWORD);

 String result = securityService.login(username, password);
 return result;
}

I ran a curl and the curl hit my web service, so I'm back to my Angular app as to why it's not sending the data as a post.

Thanks to Abhijit Sarkar for suggesting the curl. I'd forgotten about that and it shed light on the problem.

Looking at your code @RequestParam(NetworkKeyNames.KEY_PAYLOAD) . I'm guessing NetworkKeyNames.KEY_PAYLOAD has a string value. Spring will attempt to use that string value as the parameter name, so for example if NetworkKeyNames.KEY_PAYLOAD = "pay"; what you need to send to your controller is pay=testing .

Or you could just change:

@RequestParam(NetworkKeyNames.KEY_PAYLOAD) String payload

to

 @RequestParam String payload

this will use payload as the parameter name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM