SpringBoot入门笔记02——简单http接口开发实战

转自:读不懂的答案

上一篇:SpringBoot入门笔记

开始写代码

一、创建一个简单的接口,返回json

1、创建相应的包和类
一般我们会分包进行创建,我这里简单创建了一个controller 的包,里面写相关的接口controller
然后再test包下我创建了一个Application 类,这个名词可以自己起。这个类是运行springboot的入口,需要进行配置下。
mark
Application 代码如下:

2、Application 类代码讲解

package com.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args){
        SpringApplication.run(Application.class,args);
    }
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

@SpringBootApplication 注解:

@SpringBootApplication注解一般放在项目的一个启动类上,用来把启动类注入到容器中,用来定义容器扫描的范围,用来加载classpath环境中一些bean
@SpringBootApplication = @Configuration+@EnableAutoConfiguration+@ComponentScan
springboot 把这几个注解整合了。只需要写一个就可以

然后在main方法中添加SpringApplication.run(Application.class,args); 来启动应用程序

3、TestController类讲解

package com.test.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@RestController
public class TestController {
    //测试用的集合
    Map<String,Object> params=new HashMap<>();

    /**
     * 第一个接口测试
     * @param name
     * @return
     */
    @RequestMapping("/test")
    public Object getTest(String name){
        params.clear();
        params.put("name",name);
        return  params;
    }
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

这里用到了两个注解,@RestController、 @RequestMapping
@RestController and @RequestMapping是springMVC的注解,不是springboot特有的
(1)@RestController
@RestController 是spring4 新加入的注解,相当于@Controller +@ResponseBody 两个注解的功能和
@Controller的作用表示该类是一个控制器,可以接受用户的输入并调用模型和视图去完成用户的需求。控制器本身不输出也不处理任何东西。 我们写的接口也就是控制器里面的方法。
@ResponseBody 表示 请求以json映射的方式返回。
所以@RestController 注解的类就可以 接受请求——返回json
2、@RequestMapping
这个注解可以加载类和方法上,我理解是表示资源的映射吧,为web请求指明路径。可以定义不同的映射规则
注解在方法上表示当前方法时一个web请求的处理方法,注解在类上,应该是常用的请求地址或路由啥的

上面方法中再@RequestMapping(“/test”) 方法中的”/test” 就是外部访问的接口地址,暂时我们没有指定请求方法
这样外部就能通过 http://localhost:8080/test?name=张三 来进行调用这个接口。
mark

二、 get、post请求实战

(1) get请求实战

package com.test.controller;

import com.test.domain.User;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
public class GetController {

    Map<String, Object> params = new HashMap<>();

    /**
     * 从路径中取值
     *
     * @param cityID
     * @param userID
     * @return
     */
    @RequestMapping(path = "/{city_id}/{user_id}")
    public Object getUserID(@PathVariable("city_id") String cityID,
                            @PathVariable("user_id") String userID) {

        params.clear();
        params.put("cityID", cityID);
        params.put("userID", userID);
        return params;
    }

    /**
     * 测试@GetMapping注解,以get方式请求接口
     * @param id
     * @param name
     * @return
     */
    @GetMapping(value = "/v1/getUser")
    public Object getMappingTest(String id, String name) {

        params.clear();
        params.put("name", name);
        params.put("id", id);
        return params;
    }

    /**
     * 测试,设置参数默认值,别名、以及是否必传参数
     * @param id
     * @param username
     * @return
     */
    @RequestMapping("/v1/getUser1")
    public Object getMyappingTest1(@RequestParam(defaultValue = "888",name = "uid") String id, @RequestParam(required = true) String username){
        params.clear();
        params.put("username", username);
        params.put("id", id);
        return params;
    }
    /**
     * 功能描述:bean对象传参
     * 注意:1、注意需要指定http头为 content-type为application/json
     *      2、使用body传输数据
     * @param user
     * @return
     */
    @RequestMapping("/v1/save_user")
    public Object saveUser(@RequestBody User user){
        params.clear();
        params.put("user", user);
        return params;
    }

    /**
     * 功能描述:测试获取http头信息
     * @param accessToken
     * @param id
     * @return
     */
    @GetMapping("/v1/get_header")
    public Object getHeader(@RequestHeader("access_token") String accessToken, String id){
        params.clear();
        params.put("access_token", accessToken);
        params.put("id", id);
        return params;
    }

    /**
     * 以HttpServletRequest获取所有请求数据
     * @param request
     * @return
     */
    @GetMapping("/v1/test_request")
    public Object testRequest(HttpServletRequest request){

        params.clear();
        String id = request.getParameter("id");
        params.put("id",id);
        return params;
    }
    @PostMapping("/v1/login")
    public Object login(@RequestParam(required = true) String  userName, @RequestParam(required = true)String passWrod){
        params.clear();
        params.put("name",userName);
        params.put("pwd",passWrod);
        return params;
    }

    @PutMapping("/v1/put")
    public Object put(String id){
        params.clear();
        params.put("id", id);
        return params;
    }


    @DeleteMapping("/v1/del")
    public Object del(String id){
        params.clear();
        params.put("id", id);
        return params;
    }
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
注解简介:       1、单一参数@RequestMapping(path = "/{id}", method = RequestMethod.GET)
                1) public String getUser(@PathVariable String id ) {}

                2@RequestMapping(path = "/{depid}/{userid}", method = RequestMethod.GET) 可以同时指定多个提交方法
                getUser(@PathVariable("depid") String departmentID,@PathVariable("userid") String userid)

                3)get、post、put、delete四种注解的封装
                @GetMapping = @RequestMapping(method = RequestMethod.GET)
                @PostMapping = @RequestMapping(method = RequestMethod.POST)
                @PutMapping = @RequestMapping(method = RequestMethod.PUT)
                @DeleteMapping = @RequestMapping(method = RequestMethod.DELETE)

                4@RequestParam(value = "name", required = true)
                    可以设置默认值,可以设置别名name="",可以设置是否必传 requeid=truefalse

                4)@RequestBody 请求体映射实体类,以json映射javaBean
                    需要指定http头为 content-type为application/json charset=utf-8

                5@RequestHeader 请求头,比如鉴权,可以获取请求头
                    @RequestHeader("access_token") String accessToken

                6)HttpServletRequest request自动注入获取参数,可以拿到任何请求数据


  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

下一篇:SpringBoot入门笔记03——文件上传

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值