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分

  • 相关阅读:
    python 操作数据库
    python学习
    Java学习(十)
    Java学习(九)
    Java学习(八)
    Java学习(七)
    Java学习(六)
    Java学习(五)
    Java学习(四)
    Java学习(三)
  • 原文地址:https://www.cnblogs.com/heyang78/p/11406100.html
Copyright © 2011-2022 走看看