반응형
package com.example.demo;

import org.springframework.web.bind.annotation.*;

//해당클래스는 restapi를 처리하는 컨트롤러
@RestController
@RequestMapping("/api") // uri지정
public class ApiController {

    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

    @GetMapping(path = "/hello2")
    public String hell2() {
        return "hello2";
    }

    @GetMapping( "/{name}")
    public String hell2123(@PathVariable(name="name") String PathName) {
        return PathName;
    }

    @GetMapping( "/{name}")
    public String hell2123qwe(@PathVariable(name="name") String PathName) {
        return PathName;
    }

    @RequestMapping(path = "/hello3", method = RequestMethod.GET)
    public String hell3() {
        return "hello3";
    }
}

그냥 겟 메소드 만드는 여러가지 방법들

 

package com.example.demo;

import org.springframework.web.bind.annotation.*;

import java.util.Map;


//해당클래스는 restapi를 처리하는 컨트롤러
@RestController
@RequestMapping("/api") // uri지정
public class ApiController {

    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

    @GetMapping(path = "/hello2")
    public String hell2() {
        return "hello2";
    }

    @GetMapping("/{name}")
    public String hell2123(@PathVariable(name = "name") String PathName) {
        return PathName;
    }

    @GetMapping(path = "query-param")
    public String queryParam(@RequestParam Map<String, String> queryParam)

    {
        StringBuilder sb = new StringBuilder();
        queryParam.entrySet().forEach(entry ->{
            sb.append(entry.getKey() + entry.getValue());
        });


        return sb.toString();
    }

    @RequestMapping(path = "/hello3", method = RequestMethod.GET)
    public String hell3() {
        return "hello3";
    }
}
    @GetMapping(path = "query-param03")
    public String queryParam03(UserRequest userRequest)

    {
//계속 많아지니까 DTO로 매핑

        return userRequest.getName() + userRequest.getAge();
    }
    
    
    
    package com.example.demo.DTO;

public class UserRequest {
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    private String name;
    private String email;
    private int age;
}

 

rest controller 안붙이면, 그냥 static에서 페이지찾음.

안그러면 @responsebody붙여줘야함.

반응형

+ Recent posts