• Let a mthod in RestControl return a json string


    The get method of EmpControl

    package com.hy.empcloud;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.MediaType;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    @RestController
    public class EmpControl {
        @Autowired
        EmpService service;
        
        @Autowired
        RestTemplate restTemplate;
        
        @GetMapping("/test")
        public String test() {
            return "Hello";
        }
    
        @GetMapping("/emp/{id}")
        public Emp get(@PathVariable Long id) throws Exception{
            return this.service.find(id);
        }
        
        @RequestMapping(value="/all",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
        public List<Emp> get() {
            return this.service.getAll();
        }
    }

    Visit url and it's returned value

    [{"id":1,"name":"Andy","age":41},{"id":2,"name":"刘德华","age":42},{"id":3,"name":"Bill","age":43},{"id":4,"name":"比尔","age":44}]

    --END--

    2019年8月24日21点09分

  • 相关阅读:
    Android 布局中 如何使控件居中
    VGA, QVGA, HVGA, WVGA, FWVGA和iPhone显示分辨率
    [转+整理] Android 分辨率,密度,像素单位说明
    多线程的知识点总结
    集合的相关信息
    spring cloud详解
    iostat实时监控磁盘util
    Jenkins安装过程
    hdfs的block为什么设置成128M
    shell变量自增的几种方式
  • 原文地址:https://www.cnblogs.com/heyang78/p/11406100.html
Copyright © 2020-2023  润新知