zoukankan      html  css  js  c++  java
  • 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分

  • 相关阅读:
    php 对象转数组
    一张图解析FastAdmin中的表格列表的功能
    tp中打印sql,查看语句信息
    fastadmin 增加外键表搜索
    fastadmin 后台管理中,权限设置,不同管理员,显示不同的数据
    Python exec 函数
    Python eval() 函数
    Python next() 函数
    Python iter() 函数
    Python range() 函数
  • 原文地址:https://www.cnblogs.com/heyang78/p/11406100.html
Copyright © 2011-2022 走看看