SpringMVC 函數返回值問題,返回Json解決方法

SpringMVC請求處理方法的返回值:

ModelAndView
Model
Map包含模型和屬性
View
代表邏輯視圖名的String
void
提供對Servlet的訪問,以響應Http頭部和內容的HttpEntity或ResponseEntity
Callable
DeferredResult
其他任意類型,Spring將其視作輸出給View的對象模型

返回Json:

package com.fieldsignserver.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

@Controller
@RequestMapping(value="/admin")
public class LoginController {
    @ResponseBody
    @RequestMapping(value="/login",method=RequestMethod.GET)
    public ModelAndView login(@RequestParam("username") String username,@RequestParam("password") String password){
        Map result = new HashMap<String,String>();
        System.out.println("Username : "+username+", Password :"+password);
        result.put("result", "success");
        return new ModelAndView(new MappingJacksonJsonView(),result);
    }
}
發佈了47 篇原創文章 · 獲贊 55 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章