簡體   English   中英

找不到類型為com.google.gson.JsonArray的返回值的轉換器

[英]No converter found for return value of type: class com.google.gson.JsonArray

我正在開發一個應用程序,其中有兩個下拉列表。 當我從第一個下拉列表,第二個下拉列表中選擇一個值時,控制器將發生ajax調用,並且返回的列表應填充在第二個下拉列表中。 我試過下面的代碼。

jsp形式:

    <form:form modelAttribute="place" action="getWeather">
        <table>
            <tr>
                <td><form:label path="country">Country:</form:label></td>
                <td>

                    <form:select path="country" id = "countryList">
                        <form:option value="#">--Select--</form:option>
                        <form:options items="${CountriesList}"/>
                    </form:select>
                </td>
            </tr>
            <tr>
                <td><form:label path="city">City:</form:label></td>
                <td>
                    <form:select path="city" id = "cityList">

                    </form:select>
                </td>
            </tr>
        </table>

    </form:form>
<script type="text/javascript">
document.getElementById("countryList").addEventListener("change", myFunction);

/* $(document).ready(function(){
alert("sample msg");    
}); */
function myFunction(){

    var id = $(this).val()
    /* var Country = { "Country" : id }
    alert(Country);
    alert(JSON.stringify(Country)); */
    $.ajax({
        type: "GET",
        url: 'GetCities/' + id,
        dataType: 'json',
        success: function(data){
            var slctSubcat=$('#cityList'), option="";
            slctSubcat.empty();

            for(var i=0; i<data.length; i++){
                option = option + "<option value='"+data[i] + "'>"+data[i] + "</option>";
            }
            slctSubcat.append(option);
        }
    });
}
</script>

控制器方式:

@RequestMapping(value = "/GetCities/{country}", method = RequestMethod.GET)
    public @ResponseBody JsonArray getCities(@PathVariable(value="country") String Country){
        List<String> cities = getWeatherService.getCities(Country);
        Gson gson = new Gson();
        JsonElement element = gson.toJsonTree(cities, new TypeToken<List<String>>() {}.getType());
        JsonArray jsonArray = element.getAsJsonArray();
        return jsonArray;
    }

Ajax調用成功,並且正在列表中獲取數據。 但是,當以JsonArray的形式將列表傳遞給jsp時,它將給出如下異常。

org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class com.google.gson.JsonArray
    org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:225)
    org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:182)
    org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:82)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:119)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:870)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:776)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:870)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

我嘗試過直接傳遞列表,但是同樣的問題。 請幫助我找到解決方案。

終於能夠解決這個問題。 我沒有從控制器返回JsonArray,而是返回了Json字符串。 有效。

@RequestMapping(value = "/GetCities/{country}", method = RequestMethod.GET)
    public @ResponseBody String getCities(@PathVariable(value="country") String Country){
        List<String> cities = getWeatherService.getCities(Country);
        Collections.sort(cities);
        Gson gson = new Gson();
        String json = gson.toJson(cities);
        return json;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM