zoukankan      html  css  js  c++  java
  • SpringBoot学习笔记#1 创建一个RESTful Web服务

    新建一个spring boot工程

    新建一个POJO类

    public class Greeting {
        private final long id;
        private final String content;
    
        public Greeting(long id, String content) {
            this.id = id;
            this.content = content;
        }
    
        public long getId() {
            return id;
        }
    
        public String getContent() {
            return content;
        }
    }
    

    新建一个controller

    import java.util.concurrent.atomic.AtomicLong;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class GreetingController {
    
        private static final String template = "Hello, %s!";
        private final AtomicLong counter = new AtomicLong();
    
        @RequestMapping("/greeting")
        public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
            return new Greeting(counter.incrementAndGet(),
                                String.format(template, name));
        }
    }
    

    *@RestController注释标志这个类的所有方法都返回一个领域对象而不是视图,是@Controller和@ResponseBody结合的简写

    *Spring自动使用MappingJackson2HttpMessageConverter将Greeting示例转为JSON

    启动工程

    修改项目根路径

    application.properties

    spring.mvc.servlet.path=/acme
    

    访问

  • 相关阅读:
    UOS安装gitlab
    docker中连接sqlserver 2008R2(SP3) 失败解决方法
    AtCoder AGC020 E-Encoding Subsets
    CF1553H XOR and Distance
    CF1039E Summer Oenothera Exhibition
    HDOJ 6991 Increasing Subsequence
    HDOJ 6964 I love counting
    Topcoder 15279 SpanningSubgraphs
    全程NOIP计划 题目选做
    分层图总结
  • 原文地址:https://www.cnblogs.com/sunang/p/11375189.html
Copyright © 2011-2022 走看看