.com.example.demo.service
RandomNumberService.java
package com.example.demo.service; import org.springframework.stereotype.Service; @Service public class RandomNumberService { public int zeroToNine() { return (int)(Math.random() * 10); } }
HelloController.java
package com.example.demo; import java.util.Collections; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.example.demo.service.RandomNumberService; @RestController public class HelloController { @Autowired RandomNumberService random; @RequestMapping("/random") public Map<String, Integer>random(){ int value = random.zeroToNine(); return Collections.singletonMap("value", value); } }
http://localhost:8080/random
{“value”:2}
autowiredで呼び出すのか。。