003-Spring-Guide

一:步骤

1-:在https://start.spring.io/新建项目,其中,Dependencies选择“Spring Web”;

image.png

二:相关命令

./mvnw spring-boot:run     启动项目

三:简单的Spring例子:

package com.time.message;

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

@SpringBootApplication
@RestController
public class MessageApplication {

    public static void main(String[] args) {
        SpringApplication.run(MessageApplication.class, args);
    }

    @GetMapping("/hello")
    public String hello(@RequestParam(value = "name",defaultValue = "World") String name){
        return  String.format("Hello %s!",name);
    }
}

四:RESTful Web Service

package com.time.message.restservice;

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

import java.util.concurrent.atomic.AtomicLong;

@RestController
public class GreetingController {
    private static final String template = "Hello %s!";
    private final AtomicLong counter = new AtomicLong();

    @GetMapping("/greeting")
    public Greeting greeting(@RequestParam(value = "name",defaultValue = "World") String name){
        return  new Greeting(counter.incrementAndGet(),String.format(template,name));
    }
}

其中需要关注:

  @GetMapping("/greeting")                GET请求方式
  @PostMapping("/greeting")             POST请求方式
  @RequestMapping("/greeting")        GET/POST
  @RequestMapping(method=GET)    设置为GET

 即为: @RequestMapping(value = "/greeting",method = RequestMethod.GET)

五:Scheduling Tasks

package com.time.message.restservice;

import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ScheduledTasks {
    private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Scheduled(fixedRate = 5000)
    public void reportCurrentTime() {
        log.info("The time is now {}", dateFormat.format(new Date()));
    }
}

同时,需要在Application中,添加注解-“EnableScheduling”:

package com.time.message;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,256评论 19 139
  • https://docs.spring.io/spring-native/docs/current/referen...
    天高s阅读 29,736评论 0 14
  • 1. Spring Boot是什么? Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的...
    fumi106阅读 197评论 0 0
  • 表情是什么,我认为表情就是表现出来的情绪。表情可以传达很多信息。高兴了当然就笑了,难过就哭了。两者是相互影响密不可...
    Persistenc_6aea阅读 126,476评论 2 7
  • 16宿命:用概率思维提高你的胜算 以前的我是风险厌恶者,不喜欢去冒险,但是人生放弃了冒险,也就放弃了无数的可能。 ...
    yichen大刀阅读 6,135评论 0 4