处理器返回值 数值 字符串 自定义 map list 类型

 

返回数值
@Controller
public class returnObject {
    @RequestMapping("/first")
    @ResponseBody
    public Object doFirst()
    {
        return 1;
    }
//返回字符串
    @RequestMapping(value = "/str")
    @ResponseBody
    public String dos()

    {
        return "么么哒";
    }

           
public class userInfo {

    private String name;
    private int  age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
// /处理器返回值 List
    @RequestMapping(value = "lis")
    @ResponseBody
    public  Object  doList(){
        List<userInfo> list=new ArrayList<userInfo>();
        userInfo info=new userInfo();
        info.setName("啦啦");
        info.setAge(12);
        return list;
    }
 
 @RequestMapping(value = "/third")
    @ResponseBody
    public Object doThird(){
        UserInfo info=new UserInfo();
        info.setName("星星");
        info.setAge(20);
        return info;
    }


    @RequestMapping(value = "/four")
    @ResponseBody
    public Object doFour(){
        Map<String,UserInfo> map=new HashMap<String, UserInfo>();
        UserInfo info=new UserInfo();
        info.setName("微冷的雨");
        info.setAge(20);


        UserInfo info2=new UserInfo();
        info2.setName("微冷的雨2");
        info2.setAge(20);


        map.put(info.getName(),info);
        map.put(info2.getName(),info2);
        return map;
    }
    @RequestMapping(value = "/five")
    @ResponseBody
    public Object doFive(){
       List<UserInfo> list=new ArrayList<UserInfo>();
        UserInfo info=new UserInfo();
        info.setName("微冷的雨");
        info.setAge(20);
       list.add(info);
        return list;
    }
需要导入的两个jar包
 
 
  <dependency>       <groupId>com.fasterxml.jackson.core</groupId>       <artifactId>jackson-core</artifactId>       <version>2.8.1< ersion>     </dependency>     <dependency>       <groupId>com.fasterxml.jackson.core</groupId>       <artifactId>jackson-databind</artifactId>       <version>2.5.1< ersion>     </dependency>
package cn.happy.controller06exception; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping
e cn.happy.controller05requestdirect; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; /**  * Created by Happy on 2017-08-20.  */ @Controller public class FirstController {     @RequestMapping("/add")      public String addUser(){          return "/WEB-INF/insert.jsp";      }     @RequestMapping("/addUser")     public String addRealUser(HttpServletRequest request){         request.setAttribute("happy","goodperson");         return "/listUser";     }     @RequestMapping("/listUser")     public String list(){         return "/WEB-INF/list.jsp";     }     @RequestMapping("/addUser2")     public String addRealUser2(HttpServletRequest request){         request.setAttribute("happy","goodperson");         return "redirect:/listUser2";     }     @RequestMapping("/listUser2")     public String list2(){         return "redirect:/list2.jsp";     } }
@Controller public class FirstController {     @RequestMapping("/list")     public String list(){         //构造异常         int result=5/0;         return "/WEB-INF/list.jsp";     } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值