SSM接收前端传来的JSON数据并返回JSON格式的数据

前台页面login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


    loginc
    


用户名:
密码:

反馈信息的实体类ResultInfo.java如下:

package pojo;

public class ResultInfo {
    private int ok;
    private String msg;

    public ResultInfo(int ok, String msg) {
        this.ok = ok;
        this.msg = msg;
    }

    public int getOk() {
        return ok;
    }

    public void setOk(int ok) {
        this.ok = ok;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

封装的前台信息实体类User.java如下:

package pojo;


public class User {
    private  int uid;
    private String name;
    private String password;

    public User() {
    }

    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public String getName() {
        return name;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

第一种方式:利用封装好的实体类进行接收。

这里使用的实体类是User类。

控制器的代码如下:

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.ResultInfo;
import pojo.User;

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/login")
    @ResponseBody
    public ResultInfo login(@RequestBody User user) {
        System.out.println("用户名:" + user.getName() + "->" + "密码:" + user.getPassword());
        if (user.getName().equals("admin") && user.getPassword().equals("admin")) {
            return new ResultInfo(1, "登录成功");
        } else {
            return new ResultInfo(0, "登录失败");
        }
    }
}

即在方法内传@RequestBody User user。封装的实体类即可。

浏览器查看:

SSM接收前端传来的JSON数据并返回JSON格式的数据_第1张图片

第二种方式:利用@RequestBody Map map接收

即在方法传@RequestBody Map map参数,在方法体内使用map接收值。

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.ResultInfo;

import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/login")
    @ResponseBody
    public ResultInfo login(@RequestBody Map map) {
        // 使用map获取数据
        String name = map.get("name");
        String password = map.get("password");
        System.out.println("用户名:" + name + "->" + "密码:" + password);
        if (name.equals("admin") && password.equals("admin")) {
            return new ResultInfo(1, "登录成功");
        } else {
            return new ResultInfo(0, "登录失败");
        }
    }
}

 

你可能感兴趣的:(#,SSM,SSM,JSON)