Spring mvc框架学习07-01

一、请求服务器端返回的简单字符串
1、创建获取字符串控制器 - GetStringController
在这里插入图片描述

package net.tjl.spring.contorller;

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

@Controller
public class GetStringController {
    @RequestMapping(value = "/getString" , produces = "text/html;charset=utf-8")
    @ResponseBody
    public String getString(){
        return "<h1>Spring MVC真有意思!</h1>";
    }
}

运行效果
在这里插入图片描述二、请求服务器端返回的JSON数据
1、创建用户实体类 - User
在这里插入图片描述

package net.tjl.spring.contorller.bean;

import java.util.Date;

public class User {
    private int id;
    private String username;
    private String password;
    private String telephone;
    private Date registerTime;
    private int popedom;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

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

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public Date getRegisterTime() {
        return registerTime;
    }

    public void setRegisterTime(Date registerTime) {
        this.registerTime = registerTime;
    }

    public int getPopedom() {
        return popedom;
    }

    public void setPopedom(int popedom) {
        this.popedom = popedom;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", telephone='" + telephone + '\'' +
                ", registerTime=" + registerTime +
                ", popedom=" + popedom +
                '}';
    }
}
        

2、创建获取JSON控制器 - GetJsonController
在这里插入图片描述

package net.tjl.spring.contorller;

import net.tjl.spring.contorller.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class GetJsonController {
    @RequestMapping(value = "/getJson", produces = "application/json; charset=utf-8")
    public @ResponseBody
    User getJson() {
        User user = new User();
        user.setId(1);
        user.setUsername("萌萌哒");
        user.setPassword("12345");
        user.setTelephone("15889895678");
        user.setRegisterTime(new Date());
        user.setPopedom(1);
        return user;
    }

    @RequestMapping(value = "/getJsonArray", produces = "application/json; charset=utf-8")
    public @ResponseBody
    List<User> getJsonArray() {
        List<User> users = new ArrayList<User>();

        User user = new User();
        user.setId(1);
        user.setUsername("萌萌哒");
        user.setPassword("12345");
        user.setTelephone("15889895678");
        user.setRegisterTime(new Date());
        user.setPopedom(1);
        users.add(user);

        user = new User();
        user.setId(2);
        user.setUsername("李文霞");
        user.setPassword("45678");
        user.setTelephone("13978783456");
        user.setRegisterTime(new Date());
        user.setPopedom(1);
        users.add(user);

        user = new User();
        user.setId(3);
        user.setUsername("郑智化");
        user.setPassword("88888");
        user.setTelephone("15890903456");
        user.setRegisterTime(new Date());
        user.setPopedom(1);
        users.add(user);

        return users;
    }
}

3、在pom.xml文件里添加对json的支持

<!--对json的支持-->                                        
<dependency>                                               
    <groupId>com.fasterxml.jackson.core</groupId>          
    <artifactId>jackson-core</artifactId>                  
    <version>2.9.7</version>                               
</dependency>                                              
<dependency>                                               
    <groupId>com.fasterxml.jackson.core</groupId>          
    <artifactId>jackson-databind</artifactId>              
    <version>2.9.7</version>                               
</dependency>                                              
<dependency>                                               
    <groupId>com.fasterxml.jackson.core</groupId>          
    <artifactId>jackson-annotations</artifactId>           
    <version>2.9.7</version>                               
</dependency>                                              

4、启动服务器,查看结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值