SpringBoot简单程序实现一个json数据返回
  TEZNKK3IfmPf 2023年11月14日 17 0
Application :
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;

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

 

json数据bean:

package com.example.demo;

public class JsonData {
    public long id;
    public String data = "";

    public JsonData(long id, String data) {
        this.id = id;
        this.data = data;
    }

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

    public long getId() {
        return id;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getData() {
        return data;
    }
}

 

控制器controller:

package com.example.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class JsonTestController {
    @GetMapping("/jsontest")
    public JsonData myjsontest(@RequestParam(value = "name", defaultValue = "null") String name) {
        return new JsonData(2021, "zhangphil");
    }
}

注意,路由和函数名一一对应。

启动程序,在浏览器输入: 

http://localhost:8080/jsontest

页面返回内容:

{"id":2021,"data":"zhangphil"}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月14日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年05月17日   46   0   0 JSpspring
  TEZNKK3IfmPf   2024年05月17日   52   0   0 json
  TEZNKK3IfmPf   2024年04月26日   41   0   0 json
  TEZNKK3IfmPf   2024年05月17日   50   0   0 jsonmysql
TEZNKK3IfmPf