zoukankan      html  css  js  c++  java
  • 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毕竟有大神带路让我少走很多弯路

  • 相关阅读:
    JAVA写入文本文件
    oracle误删数据闪回
    Myeclipese :Creation of element failed解决方法
    Hibernate的四种状态
    java中list、set和map 的区别<转>
    C#操作mysql中临时表不自动删除
    WPF 实现地图的移动和滚动放大
    c# 将十六进制字符串写入注册表
    ASP.NET 视图状态概述:初步了解
    vs好用插件
  • 原文地址:https://www.cnblogs.com/gudulijia/p/6729154.html
Copyright © 2011-2022 走看看