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畢竟有大神帶路讓我少走很多彎路


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM