首页 > 代码库 > 2、springboot返回json

2、springboot返回json

新建maven项目

添加依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

一个User类

public class User {

    private String name;
    private int age;
    private String address;
    
    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;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}

一个Controller

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController  
@RequestMapping("/")
public class UserController {
    
    @RequestMapping("/testJson")
    public User testJson() {
        User user = new User();  
        user.setName("lijia");
        user.setAge(27);
        user.setAddress("北京");
        return user;  
    }  
     
}

一个启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ApplicationApp {
    
    public static void main(String[] args) {
        
        SpringApplication.run(ApplicationApp.class, args);
    }
}

启动,然后输入http://localhost:8080/testJson

技术分享

是不是很简单

 开始我不知道从哪里下手,真好看到了http://412887952-qq-com.iteye.com/category/356333,准备照着da毕竟有大神带路让我少走很多弯路

2、springboot返回json