通过参考链的java.lang.UnsupportedOperationException:java.util.HashMap

时间:2018-12-19 14:03:20

标签: java spring-mvc jackson

我有一个简单的spring mvc代码,如下所示,

@RequestMapping(value = "/swxapps", method = RequestMethod.POST)
    public ModelAndView createSWXApps(
            @RequestParam(value = "customerid", required = true) long customerId,
            @RequestBody String requestBody,
            HttpServletRequest request, HttpServletResponse response) throws Exception{
            Map<String, Object> model = new HashMap<String, Object>();
            model.put(modelAndViewData.getModelAttributeName(),
            swxAppsService.createSWXApps(customerId, requestBody));
            return new ModelAndView(modelAndViewData.getViewName(), model);
    }

我向此方法调用触发了一个简单的POST请求,并期望在json下方返回以下内容,

{results={"_id":16593,"Created At":"Wed Dec 19 19:16:32 IST 2018"}}

但是在POSTMAN中我得到的回报低于staacktrace

<html>
    <head>
        <title>Apache Tomcat/6.0.44 - Error report</title>
        <style>
            <!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}-->
        </style>
    </head>
    <body>
        <h1>HTTP Status 500 - (was java.lang.UnsupportedOperationException) (through reference chain: java.util.HashMap[&quot;results&quot;]-&gt;com.google.gson.JsonObject[&quot;asLong&quot;])</h1>
        <HR size="1" noshade="noshade">
        <p>
            <b>type</b> Exception report
        </p>
        <p>
            <b>message</b>
            <u>(was java.lang.UnsupportedOperationException) (through reference chain: java.util.HashMap[&quot;results&quot;]-&gt;com.google.gson.JsonObject[&quot;asLong&quot;])</u>
        </p>
        <p>
            <b>description</b>
            <u>The server encountered an internal error that prevented it from fulfilling this request.</u>
        </p>
        <p>
            <b>exception</b>
            <pre>org.codehaus.jackson.map.JsonMappingException: (was java.lang.UnsupportedOperationException) (through reference chain: java.util.HashMap[&quot;results&quot;]-&gt;com.google.gson.JsonObject[&quot;asLong&quot;])
    org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:218)
    org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:183)
    org.codehaus.jackson.map.ser.std.SerializerBase.wrapAndThrow(SerializerBase.java:140)
    org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:158)
    org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
    org.codehaus.jackson.map.ser.std.MapSerializer.serializeFields(MapSerializer.java:262)
    org.codehaus.jackson.map.ser.std.MapSerializer.serialize(MapSerializer.java:186)
    org.codehaus.jackson.map.ser.std.MapSerializer.serialize(MapSerializer.java:23)
    org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:610)
    org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:256)
    org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:1613)
    org.springframework.web.servlet.view.json.MappingJacksonJsonView.renderMergedOutputModel(MappingJacksonJsonView.java:151)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
</pre>
        </p>
        <p>
            <b>root cause</b>
            <pre>java.lang.UnsupportedOperationException
    com.google.gson.JsonElement.getAsLong(JsonElement.java:223)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:622)
    org.codehaus.jackson.map.ser.BeanPropertyWriter.get(BeanPropertyWriter.java:483)
    org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:418)
    org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
    org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
    org.codehaus.jackson.map.ser.std.MapSerializer.serializeFields(MapSerializer.java:262)
    org.codehaus.jackson.map.ser.std.MapSerializer.serialize(MapSerializer.java:186)
    org.codehaus.jackson.map.ser.std.MapSerializer.serialize(MapSerializer.java:23)
    org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:610)
    org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:256)
    org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:1613)
    org.springframework.web.servlet.view.json.MappingJacksonJsonView.renderMergedOutputModel(MappingJacksonJsonView.java:151)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
</pre>
        </p>
        <p>
            <b>note</b>
            <u>The full stack trace of the root cause is available in the Apache Tomcat/6.0.44 logs.</u>
        </p>
        <HR size="1" noshade="noshade">
        <h3>Apache Tomcat/6.0.44</h3>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

在stacktrace中,它给出了Json映射异常。当模型和视图映射不匹配时会发生。这里返回控制器的视图是gson View,模型是Jackson视图。