首页 新闻 会员 周边 捐助

spring 返回json的时候报错了,知道的大佬们求解答一下,谢谢!!!

0
悬赏园豆:20 [待解决问题]

这是我写的新增数据的 方法:


@RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public Object add(HttpServletRequest request, ModelMap modelMap, @RequestBody SysRoute sysRoute) {
sysRouteService.update(sysRoute, WebUtil.getCurrentUser());
return setSuccessModelMap(modelMap);
}


controller类开头我也加了注解 @RestController

数据能成功插入数据了,但是在return的时候报错了;

2019-04-11 15:50:26.884 [http-bio-60000-exec-2] DEBUG [] - Using 'application/json;charset=UTF-8', given [/] and supported [application/json;charset=UTF-8, text/html;charset=UTF-8, application/json;charset=UTF-8]
2019-04-11 15:50:26.885 [http-bio-60000-exec-2] DEBUG [] - Writing [{org.springframework.validation.BindingResult.bean=org.springframework.validation.BeanPropertyBindin (truncated)...]
2019-04-11 15:50:27.735 [http-bio-60000-exec-2] DEBUG [] - Using @ExceptionHandler public void com.kysx.core.base.BaseController.exceptionHandler(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,java.lang.Exception) throws java.lang.Exception
2019-04-11 15:50:27.736 [http-bio-60000-exec-2] ERROR [BaseController.java] - OH,MY GOD! SOME ERRORS OCCURED! AS FOLLOWS :
com.alibaba.fastjson.JSONException: write javaBean error, class org.springframework.beans.GenericTypeAwarePropertyDescriptor, fieldName : 0
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:326) ~[fastjson-1.2.23.jar:?]
at com.alibaba.fastjson.serializer.ArraySerializer.write(ArraySerializer.java:63) ~[fastjson-1.2.23.jar:?]
at com.alibaba.fastjson.serializer.ASMSerializer_4_BeanWrapperImpl.write(Unknown Source) ~[?:?]
at com.alibaba.fastjson.serializer.JSONSerializer.writeWithFieldName(JSONSerializer.java:304) ~[fastjson-1.2.23.jar:?]
at com.alibaba.fastjson.serializer.ASMSerializer_1_BeanPropertyBindingResult.write(Unknown Source) ~[?:?]
at com.alibaba.fastjson.serializer.MapSerializer.write(MapSerializer.java:251) ~[fastjson-1.2.23.jar:?]
at com.alibaba.fastjson.serializer.JSONSerializer.write(JSONSerializer.java:275) ~[fastjson-1.2.23.jar:?]
at com.alibaba.fastjson.JSON.writeJSONString(JSON.java:781) ~[fastjson-1.2.23.jar:?]
at com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter.writeInternal(FastJsonHttpMessageConverter.java:153) ~[fastjson-1.2.23.jar:?]
at com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter.write(FastJsonHttpMessageConverter.java:217) ~[fastjson-1.2.23.jar:?]
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:290) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:223) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:82) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:119) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) ~[tomcat-embed-core-7.0.47.jar:7.0.47]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) ~[tomcat-embed-core-7.0.47.jar:7.0.47]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) ~[tomcat-embed-core-7.0.47.jar:7.0.47]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) ~[tomcat-embed-core-7.0.47.jar:7.0.47]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) ~[tomcat-embed-core-7.0.47.jar:7.0.47]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) ~[tomcat-embed-core-7.0.47.jar:7.0.47]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) ~[tomcat-embed-core-7.0.47.jar:7.0.47]
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61) ~[shiro-web-1.4.0.jar:1.4.0]
at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108) ~[shiro-web-1.4.0.jar:1.4.0]
at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137) ~[shiro-web-1.4.0.jar:1.4.0]
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) ~[shiro-web-1.4.0.jar:1.4.0]
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66) ~[shiro-web-1.4.0.jar:1.4.0]
at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108) ~[shiro-web-1.4.0.jar:1.4.0]
at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137) ~[shiro-web-1.4.0.jar:1.4.0]
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) ~[shiro-web-1.4.0.jar:1.4.0]
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66) ~[shiro-web-1.4.0.jar:1.4.0]
at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449) ~[shiro-web-1.4.0.jar:1.4.0]
at org.apache.shiro.web.servlet.AbstractShiroFilter1.call(AbstractShiroFilter.java:365) [shiro−web−1.4.0.jar:1.4.0]atorg.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90) [shiro−core−1.4.0.jar:1.4.0]atorg.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83) [shiro−core−1.4.0.jar:1.4.0]atorg.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:387) [shiro−core−1.4.0.jar:1.4.0]atorg.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362) [shiro−web−1.4.0.jar:1.4.0]atorg.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) [shiro−web−1.4.0.jar:1.4.0]atorg.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357) [spring−web−5.1.5.RELEASE.jar:5.1.5.RELEASE]atorg.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270) [spring−web−5.1.5.RELEASE.jar:5.1.5.RELEASE]atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [tomcat−embed−core−7.0.47.jar:7.0.47]atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [tomcat−embed−core−7.0.47.jar:7.0.47]atorg.springframework.session.web.http.SessionRepositoryFilter.doFilterInternal(SessionRepositoryFilter.java:171) [spring−session−1.3.5.RELEASE.jar:?]atorg.springframework.session.web.http.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:80) [spring−session−1.3.5.RELEASE.jar:?]atorg.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357) [spring−web−5.1.5.RELEASE.jar:5.1.5.RELEASE]atorg.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270) [spring−web−5.1.5.RELEASE.jar:5.1.5.RELEASE]atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [tomcat−embed−core−7.0.47.jar:7.0.47]atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [tomcat−embed−core−7.0.47.jar:7.0.47]atorg.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) [spring−web−5.1.5.RELEASE.jar:5.1.5.RELEASE]atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring−web−5.1.5.RELEASE.jar:5.1.5.RELEASE]atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [tomcat−embed−core−7.0.47.jar:7.0.47]atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [tomcat−embed−core−7.0.47.jar:7.0.47]atorg.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:71) [log4j−web−2.6.1.jar:2.6.1]atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [tomcat−embed−core−7.0.47.jar:7.0.47]atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [tomcat−embed−core−7.0.47.jar:7.0.47]atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) [tomcat−embed−core−7.0.47.jar:7.0.47]atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) [tomcat−embed−core−7.0.47.jar:7.0.47]atorg.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [tomcat−embed−core−7.0.47.jar:7.0.47]atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) [tomcat−embed−core−7.0.47.jar:7.0.47]atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) [tomcat−embed−core−7.0.47.jar:7.0.47]atorg.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) [tomcat−embed−core−7.0.47.jar:7.0.47]atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) [tomcat−embed−core−7.0.47.jar:7.0.47]atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) [tomcat−embed−core−7.0.47.jar:7.0.47]atorg.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) [tomcat−embed−core−7.0.47.jar:7.0.47]atorg.apache.coyote.AbstractProtocolAbstractConnectionHandler.process(AbstractProtocol.java:603) ~[tomcat-embed-core-7.0.47.jar:7.0.47]
at org.apache.tomcat.util.net.JIoEndpointSocketProcessor.run(JIoEndpoint.java:310) [tomcat−embed−core−7.0.47.jar:7.0.47]atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)[?:1.8.0171]atjava.util.concurrent.ThreadPoolExecutorWorker.run(ThreadPoolExecutor.java:624) [?:1.8.0_171]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_171]
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_171]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_171]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_171]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_171]
at com.alibaba.fastjson.util.FieldInfo.get(FieldInfo.java:451) ~[fastjson-1.2.23.jar:?]
at com.alibaba.fastjson.serializer.FieldSerializer.getPropertyValue(FieldSerializer.java:105) ~[fastjson-1.2.23.jar:?]
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:196) ~[fastjson-1.2.23.jar:?]
... 75 more
Caused by: java.lang.IllegalStateException: No write method available
at org.springframework.util.Assert.state(Assert.java:73) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.GenericTypeAwarePropertyDescriptor.getWriteMethodForActualAccess(GenericTypeAwarePropertyDescriptor.java:137) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_171]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_171]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_171]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_171]
at com.alibaba.fastjson.util.FieldInfo.get(FieldInfo.java:451) ~[fastjson-1.2.23.jar:?]
at com.alibaba.fastjson.serializer.FieldSerializer.getPropertyValue(FieldSerializer.java:105) ~[fastjson-1.2.23.jar:?]
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:196) ~[fastjson-1.2.23.jar:?]
... 75 more
2019-04-11 15:50:28.599 [http-bio-60000-exec-2] DEBUG [] - Resolved [com.alibaba.fastjson.JSONException: write javaBean error, class org.springframework.beans.GenericTypeAwarePropertyDescriptor, fieldName : 0]
2019-04-11 15:50:28.599 [http-bio-60000-exec-2] DEBUG [] - Completed 200 OK

自然醒--的主页 自然醒-- | 初学一级 | 园豆:77
提问于:2019-04-11 16:26
< >
分享
所有回答(3)
0

把produces = "application/json;charset=UTF-8"去掉试试

无恨之都 | 园豆:287 (菜鸟二级) | 2019-04-11 22:01

我一开始没加这行就报错了,后来试着加上了这行还是一样的问题

支持(0) 反对(1) 自然醒-- | 园豆:77 (初学一级) | 2019-04-11 22:03
0

你把setSuccessModelMap()方法贴出来看一下,返回值是什么

zycyc | 园豆:285 (菜鸟二级) | 2019-04-12 14:13

protected ResponseEntity<ModelMap> setSuccessModelMap(ModelMap modelMap, Object data) {
return this.setModelMap(modelMap, HttpCode.OK, data);
}

protected ResponseEntity<ModelMap> setModelMap(ModelMap modelMap, HttpCode code, Object data) {
modelMap.remove("void");
if (data != null) {
modelMap.put("data", data);
}
modelMap.put("httpCode", code.value());
modelMap.put("msg", code.msg());
modelMap.put("timestamp", System.currentTimeMillis());
return ResponseEntity.ok(modelMap);
}

支持(0) 反对(0) 自然醒-- | 园豆:77 (初学一级) | 2019-04-12 14:47

@自然醒--: ResponseEntity类是否有getter, setter方法

支持(0) 反对(0) zycyc | 园豆:285 (菜鸟二级) | 2019-04-12 16:44

@zycyc:
ResponseEntity这个类是下面依赖包里的,

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.spring}</version>
</dependency>

我如果不用 @RequestBody的方式, 改用从request里 获取传过来的数据,然后正常插入返回是不会报错的,不知道是不是和RequestBody这个注解有关系

支持(0) 反对(0) 自然醒-- | 园豆:77 (初学一级) | 2019-04-12 17:19
0

给你个例子,自己研究下:还是具体情况具体处理的(多打几个断点看看)
1.js

其中的请求头中一定要加上:'Content-Type': 'application/json'
2.控制层(controller)

3.服务层(services)

根据自己的需求进行编写

注:返回的JsonData类只是一个简单的bean,对返回数据进行统一处理

糖炒栗 | 园豆:330 (菜鸟二级) | 2019-04-15 16:20

谢谢回答啦,我自己解决了,但是我不知道是为什么,
controller方法, 原来是这样的:
public Object addDicIndex(HttpServletRequest request, ModelMap modelMap, @RequestBody SysDicIndex record) {
return setSuccessModelMap(modelMap);
}
改为
public Object addDicIndex2(HttpServletRequest request, @RequestBody SysDicIndex record ) {
ModelMap modelMap = new ModelMap();
return setSuccessModelMap(modelMap);
}

去掉参数列表中的 ModelMap , 改为ModelMap map = new ModelMap ,
我不明白这两种写法有什么区别。改了以后就不报错了

支持(0) 反对(0) 自然醒-- | 园豆:77 (初学一级) | 2019-04-16 15:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册