我的角度应用程序正在调用Spring引导运行的REST API Web服务。以下是来自firefox Web控制台的参数转储:
我的春季启动webapp正在抛出此异常:
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]
这是我的方法......
@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;
}
很明显,它正在击中我的春季启动应用程序,但参数发生了什么?有什么方法可以获得更多信息吗?
答案 0 :(得分:0)
试试这个,我不确定你的Spring Boot版本,你可能需要调整一下。
// 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;
}
答案 1 :(得分:0)
我跑了一个卷发,卷曲打到了我的网络服务,所以我回到我的Angular应用程序,看看它为什么不把这些数据作为帖子发送。
感谢Abhijit Sarkar建议卷曲。我忘记了这一点,它揭示了这个问题。
答案 2 :(得分:0)
查看代码@RequestParam(NetworkKeyNames.KEY_PAYLOAD)
。我猜测NetworkKeyNames.KEY_PAYLOAD
有一个字符串值。 Spring将尝试使用该字符串值作为参数名称,例如,如果NetworkKeyNames.KEY_PAYLOAD = "pay";
需要发送给控制器的是pay=testing
。
或你可以改变:
@RequestParam(NetworkKeyNames.KEY_PAYLOAD) String payload
到
@RequestParam String payload
这将使用payload
作为参数名称