Spring boot 6주차 개발일지

이동규·2023년 6월 26일

Springboot 기초

목록 보기
6/13
post-thumbnail

Spring MVC(Model-View-Controller)

@Controller //요청 경로를 설정하기 위해 Controller Annotation을 사용한다
public class SampleController {
    private static  final Logger logger = LoggerFactory.getLogger(SampleController.class);
    @RequestMapping("/profile")//RequestMapping을 이용해 경로에 따라 실행될 함수를 지정 할 수 있다.http://localhost:8080/profile
    public String profile(){
        logger.info("in profile");
        return "/hello.html";
    }

    @RequestMapping(value = "/hello"// 경로의 값 Request Mapping을 이용해 경로에 따라 실행될 함수를 지정 할 수 있다.
    ,method = RequestMethod.GET
    )
    public String hello(@RequestParam(name = "id",required = false,defaultValue = "")String name)//http://localhost:8080/hello
    //http://localhost:8080/hello?id=ldkstellar
    {
        logger.info("Path : hello");
        logger.info("Query Param id:" + name);
        return "/hello.html";
    }
    @GetMapping(value = "/hello/{id}")// //http://localhost:8080/hello/ldkstellar
    public String helloPath(@PathVariable("id") String id){
        logger.info("Path Variable is " + id);
        return "/hello.html";
    }
    @GetMapping(
            "/get-profile"
    )//http://localhost:8080/get-profile
    public @ResponseBody SamplePayload getProfile(){// HTML 외에 데이터 전송을 위해 Body와 Media Type을 설정할 수 있다.
        return  new SamplePayload("ldkstellar",26,"Developer");
    }

}
@RestController// Controller + Responsebody
@RequestMapping("/rest")
public class SampleRestController {
    private static  final Logger logger = LoggerFactory.getLogger(SampleController.class);
    @GetMapping("/Sample-payload")//http://localhost:8080/rest/Sample-payload
    public  SamplePayload SamplePayload(){
        logger.info("SamplePayload complete!");
        return  new SamplePayload("ldkstellar",26,"Developer");
    }
     @GetMapping(value = "/sample-image",// HTML 외에 데이터 전송을 위해 Body와 Media Type을 설정할 수 있다.http://localhost:8080/rest/sample-image
            produces = MediaType.IMAGE_PNG_VALUE
    )
    public byte[] SampleImage() throws IOException{
        InputStream inputStream =getClass().getResourceAsStream("/static/k.PNG");

        return  inputStream.readAllBytes();
    }


}

0개의 댓글