在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ 后臺controller返回的map要如何轉換成json格式呀,不轉換前端報40

后臺controller返回的map要如何轉換成json格式呀,不轉換前端報406 (Not Acceptable)?

@RequestMapping(value = "/add_news",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map add_news (News news){
        news.setCreate_time(DateUtil.getCurrentTime());
        news.setId(SysUtil.getUUID());
        news.setState(0);
        newsService.add_news(news);
        Map<String,Object> resultMap = new HashMap<String, Object>(); 
        resultMap.put("result", "操作成功");  
        return resultMap;  
    }
$.ajax({
                url:"/ehouse/admin/add_news.html",
                data:{
                    type:type,
                    title:title,
                    content:content
                },
                dateType:"json",
                type:"post",
                success:function(data){
                    alert("成功");
                },
                error:function(){
                    alert("上傳失敗");
                }
                
            });
回答
編輯回答
久愛她

@ResponseBody默認使用jackson將你的map轉換成json。
你這里的錯誤不是你后臺寫錯。是你的$.ajax的URL寫錯
url:"/ehouse/admin/add_news"改成這個就可以了

2017年8月6日 19:18
編輯回答
舊螢火

可以試一下 這里的方法

2017年5月27日 12:58
編輯回答
玄鳥

試試fastJosn或者Gson?
<dependency>

        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.23</version>
    </dependency>

<dependency>

        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.3.1</version>
    </dependency>
2017年12月15日 07:10
編輯回答
笨尐豬
配置
<mvc:annotation-driven>
         <mvc:message-converters register-defaults="true">
         <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
              <property name="supportedMediaTypes">
                   <list>
                    <value>text/html;charset=UTF-8</value>
                    <value>application/json;charset=UTF-8</value>
                </list>
              </property>
          </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

或者修改<url-pattern>.html</url-pattern>成.do或者*.action之類,親測可用。

2017年10月12日 16:22