Spring mvc返回JSON數據的兩種配置方式

1.視圖解析方式

依賴包:jackson-core、jackson-databind、jackson-annotation
Spring配置文件內容:java

<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="defaultContentType" value="application/json;charset=utf-8" ></property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="contentNegotiationManager" ref="contentNegotiationManager"></property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
<property name="prettyPrint" value="true"/>
</bean>
</list>
</property>
</bean>

Controller代碼:web

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
    @RequestMapping(value="/test.do")
    public void test(Model m){
        m.addAttribute("test","Hello World!");
    }
}

2.@ResponseBody返回方式

依賴包:jackson-core、jackson-databind、jackson-annotation
Spring配置文件內容:spring

<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="defaultCharset" value="utf-8"></property>
</bean>
</list>
</property>
</bean>

Controller代碼:json

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;import java.util.Map;

@Controller
public class TestController {
    @ResponseBody
    @RequestMapping(value="/test.do")
    public Map test(){
        return new HashMap(){{put("aa","Hello World!");}};
    }
}
相關文章
相關標籤/搜索