4、Controller的使用

  1. 1345806-20180326081956948-899436707.png
  2. @Controller
    @ResponseBody
    1. 等同于RESTController
  3. RequestMapping的使用
    1. 给同一个方法指定多个RequestMapping
    2. 给类添加RequestMapping
  4. 指定RequestMapping的()method属性

  5. 如何处理URL里面的参数?
1345806-20180326081958805-1969385969.png
@PathVariable的使用:
  1. http://127.0.0.1:8080/hello/say/243
  2. http://127.0.0.1:8080/hello/243/say
package com.girl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by sunnyangzs on 2018/3/25.
 */

@RestController
@RequestMapping("/hello")
public class HelloController {

//    @Value("${cupSize}")
//    private String cupSize;
//
//    @Value("${age}")
//    private  Integer age;
//
//    @Value("${content}")
//    private String content;

    @Autowired
    private GirlProperties girlProperties;

    @RequestMapping(value = {"/say/{id}"},method = RequestMethod.GET)
    public String say(@PathVariable("id") Integer id){
//        return girlProperties.getCupSize();
        return "id: "+id;
    }

}
//把id写在了say的后面
35
 
1
package com.girl;
2
 
            
3
import org.springframework.beans.factory.annotation.Autowired;
4
import org.springframework.beans.factory.annotation.Value;
5
import org.springframework.stereotype.Controller;
6
import org.springframework.web.bind.annotation.*;
7
 
            
8
/**
9
 * Created by sunnyangzs on 2018/3/25.
10
 */
11
 
            
12
@RestController
13
@RequestMapping("/hello")
14
public class HelloController {
15
 
            
16
//    @Value("${cupSize}")
17
//    private String cupSize;
18
//
19
//    @Value("${age}")
20
//    private  Integer age;
21
//
22
//    @Value("${content}")
23
//    private String content;
24
 
            
25
    @Autowired
26
    private GirlProperties girlProperties;
27
 
            
28
    @RequestMapping(value = {"/say/{id}"},method = RequestMethod.GET)
29
    public String say(@PathVariable("id") Integer id){
30
//        return girlProperties.getCupSize();
31
        return "id: "+id;
32
    }
33
 
            
34
}
35
//把id写在了say的后面
package com.girl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by sunnyangzs on 2018/3/25.
 */

@RestController
@RequestMapping("/hello")
public class HelloController {

//    @Value("${cupSize}")
//    private String cupSize;
//
//    @Value("${age}")
//    private  Integer age;
//
//    @Value("${content}")
//    private String content;

    @Autowired
    private GirlProperties girlProperties;

    @RequestMapping(value = {"/{id}/say"},method = RequestMethod.GET)
    public String say(@PathVariable("id") Integer id){
//        return girlProperties.getCupSize();
        return "id: "+id;
    }

}
//把id写在了say的前面
35
 
1
package com.girl;
2
 
            
3
import org.springframework.beans.factory.annotation.Autowired;
4
import org.springframework.beans.factory.annotation.Value;
5
import org.springframework.stereotype.Controller;
6
import org.springframework.web.bind.annotation.*;
7
 
            
8
/**
9
 * Created by sunnyangzs on 2018/3/25.
10
 */
11
 
            
12
@RestController
13
@RequestMapping("/hello")
14
public class HelloController {
15
 
            
16
//    @Value("${cupSize}")
17
//    private String cupSize;
18
//
19
//    @Value("${age}")
20
//    private  Integer age;
21
//
22
//    @Value("${content}")
23
//    private String content;
24
 
            
25
    @Autowired
26
    private GirlProperties girlProperties;
27
 
            
28
    @RequestMapping(value = {"/{id}/say"},method = RequestMethod.GET)
29
    public String say(@PathVariable("id") Integer id){
30
//        return girlProperties.getCupSize();
31
        return "id: "+id;
32
    }
33
 
            
34
}
35
//把id写在了say的前面
@RequestParam的使用方式
  1. http://127.0.0.1:8080/hello/say?id=22223
package com.girl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by sunnyangzs on 2018/3/25.
 */

@RestController
@RequestMapping("/hello")
public class HelloController {

//    @Value("${cupSize}")
//    private String cupSize;
//
//    @Value("${age}")
//    private  Integer age;
//
//    @Value("${content}")
//    private String content;

    @Autowired
    private GirlProperties girlProperties;

    @RequestMapping(value = {"/say"},method = RequestMethod.GET)
    public String say(@RequestParam("id") Integer id){
//        return girlProperties.getCupSize();
        return "id: "+id;
    }

}
1
35
 
1
package com.girl;
2
 
            
3
import org.springframework.beans.factory.annotation.Autowired;
4
import org.springframework.beans.factory.annotation.Value;
5
import org.springframework.stereotype.Controller;
6
import org.springframework.web.bind.annotation.*;
7
 
            
8
/**
9
 * Created by sunnyangzs on 2018/3/25.
10
 */
11
 
            
12
@RestController
13
@RequestMapping("/hello")
14
public class HelloController {
15
 
            
16
//    @Value("${cupSize}")
17
//    private String cupSize;
18
//
19
//    @Value("${age}")
20
//    private  Integer age;
21
//
22
//    @Value("${content}")
23
//    private String content;
24
 
            
25
    @Autowired
26
    private GirlProperties girlProperties;
27
 
            
28
    @RequestMapping(value = {"/say"},method = RequestMethod.GET)
29
    public String say(@RequestParam("id") Integer id){
30
//        return girlProperties.getCupSize();
31
        return "id: "+id;
32
    }
33
 
            
34
}
35
 
            
package com.girl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by sunnyangzs on 2018/3/25.
 */

@RestController
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    private GirlProperties girlProperties;

    @RequestMapping(value = {"/say"},method = RequestMethod.GET)
    public String say(@RequestParam("id") Integer myId){
//        return girlProperties.getCupSize();
        return "id: "+myId;
    }

}
 
1
package com.girl;
2
 
            
3
import org.springframework.beans.factory.annotation.Autowired;
4
import org.springframework.beans.factory.annotation.Value;
5
import org.springframework.stereotype.Controller;
6
import org.springframework.web.bind.annotation.*;
7
 
            
8
/**
9
 * Created by sunnyangzs on 2018/3/25.
10
 */
11
 
            
12
@RestController
13
@RequestMapping("/hello")
14
public class HelloController {
15
 
            
16
    @Autowired
17
    private GirlProperties girlProperties;
18
 
            
19
    @RequestMapping(value = {"/say"},method = RequestMethod.GET)
20
    public String say(@RequestParam("id") Integer myId){
21
//        return girlProperties.getCupSize();
22
        return "id: "+myId;
23
    }
24
 
            
25
}
26
 
            
给url当中的参数设置默认值
package com.girl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by sunnyangzs on 2018/3/25.
 */

@RestController
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    private GirlProperties girlProperties;

    @RequestMapping(value = {"/say"},method = RequestMethod.GET)
    public String say(@RequestParam(value = "id",required = false,defaultValue = "0") Integer myId){
//        return girlProperties.getCupSize();
        return "id: "+myId;
    }

}
package com.girl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; /** * Created by sunnyangzs on 2018/3/25. */ @RestController @RequestMapping("/hello") public class HelloController { @Autowired private GirlProperties girlProperties; @RequestMapping(value = {"/say"},method = RequestMethod.GET) public String say(@RequestParam(value = "id",required = false,defaultValue = "0") Integer myId){ // return girlProperties.getCupSize(); return "id: "+myId; } }
x
1
package com.girl;
2
 
            
3
import org.springframework.beans.factory.annotation.Autowired;
4
import org.springframework.beans.factory.annotation.Value;
5
import org.springframework.stereotype.Controller;
6
import org.springframework.web.bind.annotation.*;
7
 
            
8
/**
9
 * Created by sunnyangzs on 2018/3/25.
10
 */
11
 
            
12
@RestController
13
@RequestMapping("/hello")
14
public class HelloController {
15
 
            
16
    @Autowired
17
    private GirlProperties girlProperties;
18
 
            
19
    @RequestMapping(value = {"/say"},method = RequestMethod.GET)
20
    public String say(@RequestParam(value = "id",required = false,defaultValue = "0") Integer myId){
21
//        return girlProperties.getCupSize();
22
        return "id: "+myId;
23
    }
24
 
            
25
}
26
 
            
组合注解的使用
  1. GetMapping
package com.girl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by sunnyangzs on 2018/3/25.
 */

@RestController
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    private GirlProperties girlProperties;

    //@RequestMapping(value = {"/say"},method = RequestMethod.GET)
    @GetMapping(value="/say")
    public String say(@RequestParam(value = "id",required = false,defaultValue = "0") Integer myId){
//        return girlProperties.getCupSize();
        return "id: "+myId;
    }

}
1
26
 
1
package com.girl;
2
 
            
3
import org.springframework.beans.factory.annotation.Autowired;
4
import org.springframework.beans.factory.annotation.Value;
5
import org.springframework.stereotype.Controller;
6
import org.springframework.web.bind.annotation.*;
7
 
            
8
/**
9
 * Created by sunnyangzs on 2018/3/25.
10
 */
11
 
            
12
@RestController
13
@RequestMapping("/hello")
14
public class HelloController {
15
 
            
16
    @Autowired
17
    private GirlProperties girlProperties;
18
 
            
19
    //@RequestMapping(value = {"/say"},method = RequestMethod.GET)
20
    @GetMapping(value="/say")
21
    public String say(@RequestParam(value = "id",required = false,defaultValue = "0") Integer myId){
22
//        return girlProperties.getCupSize();
23
        return "id: "+myId;
24
    }
25
 
            
26
}
27
 
            




转载于:https://www.cnblogs.com/sunnyangzs/p/8648461.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值