Ⅰ springmvc怎麼返回json
只需在controller上面添加註解@ResponseBody,這樣spring就會自動把輸出純帆結做攜雹果轉成json.
@ResponseBody
@RequestMapping(value = "/隱銷query")
public User queryUser(HttpServletRequest request){
User user = new User();
return user;
}
Ⅱ springmvc返回json數據需做哪些處理
Spring-mvc.xml配置
<!--啟動SpringMVC的註解功能,完成請求和註解POJO的映射-->
<beanclass="org.springframework.web.servlet.mvc.annotation.">
<propertyname="messageConverters">
<list>
<!--json轉換器-->
<refbean=""/>
</list>
</property>
</bean>
<beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<propertyname="messageConverters">
<list>
<refbean=""/>
</list>
</property>
</bean>
<beanid=""
class="org.springframework.http.converter.json.">
<propertyname="supportedMediaTypes">
<list>
<beanclass="org.springframework.http.MediaType">
<constructor-argindex="0"value="text"/>
<constructor-argindex="1"value="plain"/>
<constructor-argindex="2"value="UTF-8"/>
</bean>
<beanclass="org.springframework.http.MediaType">
<constructor-argindex="0"value="*"/>
<constructor-argindex="1"value="*"/>
<constructor-argindex="2"value="UTF-8"/>
</bean>
<beanclass="org.springframework.http.MediaType">
<constructor-argindex="0"value="text"/>
<constructor-argindex="1"value="*"/>
<constructor-argindex="2"value="UTF-8"/>
</bean>
<beanclass="org.springframework.http.MediaType">
<constructor-argindex="0"value="application"/>
<constructor-argindex="1"value="json"/>
<constructor-argindex="2"value="UTF-8"/>
</bean>
</list>
</property>
</bean>
java SpringMVC Controller方法
/**
*
*開發公司:SOJSON在線工具<p>
*版權所有:©www.sojson.com
*博客地址:http://www.sojson.com/blog/
*<p>
*
*返回JSONControllermethod
*
*<p>
*
*區分責任人日期說明
*創建周柏成2017年4月18日10:28
*
*@authorzhou-cheng
*@[email protected]
*@version1.0,2017年4月18日10:28
*
*/
@RequestMapping(value="logout",method=RequestMethod.GET)
@ResponseBody//返回JSON
publicMap<String,Object>logout(){
try{
TokenManager.logout();
resultMap.put("status",200);
}catch(Exceptione){
resultMap.put("status",500);
logger.error("errorMessage:"+e.getMessage());
LoggerUtils.fmtError(getClass(),e,"退出出現錯誤,%s。",e.getMessage());
}
returnresultMap;
}
如果是Spring4.x 請看下這里:
http://www.sojson.com/blog/145.html
Ⅲ springmvc如何返回json串和返回map的區別
spring mvc 支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void。
Map
@RequestMapping("/demo2/show")
public Map<String, String> getMap() {
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value-1");
map.put("key2", "value-2");
return map;
}
在jsp頁面中可直通過${key1}獲得到值, map.put()相當於request.setAttribute方法。
頁面上這么寫:
<c:forEach items="${map}" var="entry">
<c:out value="${entry.key}" />
<c:out value="${entry.value}" />
</c:forEach>
返回json的例子(使用Jackson):
@RequestMapping("/load1")
@ResponseBody
public String load1(@RequestParam String name,@RequestParam String password) throws IOException{
System.out.println(name+" : "+password);
//return name+" : "+password;
MyDog dog=new MyDog();
dog.setName("小哈");dog.setAge("1歲");dog.setColor("深灰");
ObjectMapper objectMapper = new ObjectMapper();
String jsonString=objectMapper.writeValueAsString(dog);
System.out.println(jsonString);
return jsonString;
}
Ⅳ spring mvc4.x怎麼返回json格式
springmvcajax返回json字元串的設置方法:使用技術及環境磨雹碰:Spring3.2.2.RELEASEJackson1.9.10JDK1.6Eclipse3.6Maven3PS:在spring3中,要輸出json數據,只需要添加Jackson 庫到你的classpath。1、項目依賴肆陵spring和jackson的依賴瞎談
Ⅳ spring mvc4.x怎麼返回json格式
Spring MVC 3.0 返回JSON數據的方法,可以使用Spring內置的支持,代碼如下:
// Spring MVC 配置
<bean class="org.springframework.web.servlet.mvc.annotation.">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.json." />
</list>
</property>
</bean>
// WEB-INF/lib 中添加
jackson-mapper-asl-1.6.4.jar
jackson-core-asl-1.6.4.jar
//Controller中使卜鍵盯用:@ResponseBody 標注亮搜方法,該方法返回 Java對象(支持復雜對型和象), 會將對象轉換為 JSON 輸出
Ⅵ 求助,spring mvc 攔截器如何獲取 controller 返回的json結果
那你必須提供一個類來定敗弊仿義前台與後台信察纖息交互卜伏的格式 不能再使用springmvc提供的默認類 這需要在配置文件中顯示指定
Ⅶ 求助,spring mvc 攔截器如何獲取 controller 返回的json結果
也許可以這樣,你需要返回一個json,這旅段個json由一個對象唄@ResponseBody自動序列生成,然後,你試著將這個對象保存進model裡面,mv.setObject("xx",xx);然後在攔截器中攔截未返回的的那個方法中model裡面的對象做處理,對象是引用,這里改了,如果@ResponseBody的返回處理還為發生,即鏈念可改變返回數據拆喚譽,至於取得,也可以將對象手動序列(找個api就行)!
Ⅷ spring mvc4.x怎麼返回json格式
SpringMVC3.0返回JSON數據的方法,可以使用Spring內置的支持,代碼如下://SpringMVC配置//WEB-INF/lib中添加jackson-mapper-asl-1.6.4.jarjackson-core-asl-1.6.4.jar//Controller中使用:@ResponseBody標注方法,該方法返回Java對象(支持復雜對象),會將對象轉換為JSON輸出
Ⅸ springmvc怎麼返回json
SpringMVC返回json數據有三種方式x0dx0a1、第一種方式是spring2時代的產物,也就是每個json視圖controller配置一個Jsoniew。如:
Ⅹ springmvc從後台怎麼同時返回多個json
返回json對象的話,你可以考慮返回的對象結構進行改變,就是把java對象轉成json對象。