如果接口注解是@RequestBody ,且修饰的是字符串参数,请求如果接受非json格式字符串参数?
最近维护公司的一个springboot项目,发现有一个controller接口写的接收参数形式很奇怪。代码如下
@ResponseBody @PostMapping(value = "/SendNews") public String sendContent(HttpServletRequest request,@RequestBody String lstMsgId) { System.out.println(lstMsgId); return lstMsgId;}
我的请求参数是:"90c8c36f23a94c1487851129aa47d690/90c8c36f23a94c1487851129aa47d690"
我用postman 请求,在raw里面带上字符串(非Json格式)可以正常请求,而使用hutool工具请求如下代码却不能请求成功。
HttpRequest request = HttpRequest.get(url); request.header("gatewayAuth", "xxxx"); responseJsonStr = request.form(null)//表单内容 .body("90c8c36f23a94c1487851129aa47d690/90c8c36f23a94c1487851129aa47d690") .timeout(50000)//超时,毫秒 .execute().body();
报如下错误:
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: not close json text, token : error; nested exception is com.alibaba.fastjson.JSONException: not close json text, token : error at com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter.readType(FastJsonHttpMessageConverter.java:273) at com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter.read(FastJsonHttpMessageConverter.java:239) at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:185) at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:160) at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:133) at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:122) at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:179) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:146) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:209) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) at org.springframework.web.filter.CorsFilter.doFilterInternal(CorsFilter.java:91) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:168) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:481) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:130) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:390) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:928) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1794) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748)Caused by: com.alibaba.fastjson.JSONException: not close json text, token : error
我的请求参数是:"90c8c36f23a94c1487851129aa47d690/90c8c36f23a94c1487851129aa47d690",但是如果改成:"\"90c8c36f23a94c1487851129aa47d690/90c8c36f23a94c1487851129aa47d690\""
使用hutool工具类请求就可以了。为什么会这样? @RequestBody String lstMsgId 怎么接收非json格式的字符串参数,求解~
先把.get(url)改为.post
"90c8c36f23a94c1487851129aa47d690/90c8c36f23a94c1487851129aa47d690"
我用postman 请求,在raw里面带上字符串(非Json格式)可以正常请求
双引号包字符 = json的字符串,所以其实还是json格式,所以可以当json解析
如何让@RequestBody
解析非json的字符串?让spring不认为请求是json就行了
spring是如何识别的呢?通过Content-Type
头
所以,发请求的时候,指定Content-Type
头是text/plain
就完事了
本文向大家介绍详解SpringMVC @RequestBody接收Json对象字符串,包括了详解SpringMVC @RequestBody接收Json对象字符串的使用技巧和注意事项,需要的朋友参考一下 页面提交请求参数有两种,一种是form格式提交,一种json格式提交 通常情况下我们使用的都是form格式提交的数据,数据格式:k=v&k=v,这个时候用springMVC接收参数没有问题,但有时
但是在scala中我需要使用: scala版本是有意义的,因为注释需要一个字符串数组。但是为什么上面的工作在java中,它不应该给出编译时错误吗? 下面的类'ArrayChecker'(我编写了一个类来说明这一点)会导致java编译时错误:
红线位于“打开导航抽屉”和“关闭导航抽屉”下 这张图片显示了这个问题
问题内容: 大家!我可以请您帮我解码以下JSON代码: 我想将上述结构组织为: 注1: 文件夹:收件箱 来自(来自):… 日期(日期):… 时间(时间):… utcOffsetSeconds:… 收件人(地址):… 收件人(姓名):… 状态(deliveryStatus):… 文字(正文):… 笔记2: … 先感谢您! 问题答案: 您可以使用该函数来解码JSON字符串: 而且您会得到像这样的东西:
问题内容: 我想创建一个函数来接收输入字符串,该字符串可以是json格式的字符串,也可以只是一个字符串。例如,以下功能很简单。 问题答案: 我不清楚您是否仅需要了解“引号字符串”,还是需要了解json或两者之间的区别,因此,这向您展示了如何检测这两种情况,因此您可以非常具体。 我也在这里发布了交互式代码示例:http : //play.golang.org/p/VmT0BVBJZ7 将输出以下内容
我正在使用一个简单的正则表达式匹配器来匹配wiketump中的一段JSON文本。JSON在XML内部。我已经检索到XML tag