LoginSignup
0
1

More than 3 years have passed since last update.

Spring Java

Last updated at Posted at 2020-02-05
  • Spring Tools 4 for Eclipseを入手し以下で解凍
java -jar spring-tool-suite-4-4.5.1.RELEASE-e4.14.0-win32.win32.x86_64.self-extracting.jar
  • 必要によってSpringToolSuite4.iniに-vm追加
-vm
C:\Program Files\Java\jdk1.8.0_211\bin\javaw.exe
  • springプロジェクト作成

File → New → Spring Starter Project → Next →
web機能とホットデプロイを使うため以下をチェック
1. Developer Tools→Spring Boot DevTools
2. Web→Spring Web
→ Finish

new.png

pom.xml
<dependencies>
・・・
 <!-- web -->
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <!-- ホットデプロイ -->
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
  <scope>runtime</scope>
  <optional>true</optional>
 </dependency>
・・・
</dependencies>
IndexController.java


package com.example.demo;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

    @RequestMapping("/")
    private String index() {
        return "index";
    }

    @RequestMapping(value="/", method=RequestMethod.GET)
    private Map<String, Object> index( @RequestParam("id") Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        return map;
    }

    @RequestMapping(value="/items/{id}", method=RequestMethod.GET)
    private Item get(@PathVariable int id) {
        return new Item(id);
    }

    @RequestMapping(value="/items", method=RequestMethod.POST)
    private void post(@RequestBody String body) {
        log.info("post, " + body);
    }

    @RequestMapping(value="/items/{id}", method=RequestMethod.PUT)
    private void put(@PathVariable int id, @RequestBody String body) {
        log.info("put, " + id + ", " + body);
    }

    @RequestMapping(value="/items/{id}", method=RequestMethod.DELETE)
    private void delete(@PathVariable("id") int id) {
        log.info("delete, " + id);
    }

    public static class Item {
        private int id;

        Item (int id) {
            this.setId(id);
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }
    }
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1