spring学习------一些常用注解的加深(一)

1.@RestController与@Controller的区别

在spring的注解@RestController相当于@Controller与@ResponseBody的合并,代码如下:

Greeting.class

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

GreetingController.class

@RestController
public class GreetingController {

    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(1,"helloWord");
    }
}

即相当于

@Controller
public class GreetingController {

    @RequestMapping("/greeting")
    @ResponseBody
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(1,"helloWord");
    }
}

你可能感兴趣的:(spring)