SpringBoot之Post请求

上一篇写的是使用GET请求获取数据。这一篇试试Post请求获取数据,创建一个Java文件BusController:

屏幕快照 2018-12-25 下午2.45.08.png

然后是编码:

package com.bianla.demotestspringboot;

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

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

@RestController

public class BusController {

    //创建一个查询的helper

    private BusQueryHelper helper = new BusQueryHelper();

    //声明api的接口,请求方式

    @RequestMapping(value = "api/queryBus",method = RequestMethod.POST)

    //使用busParam对象来接收请求体

    public Object CheckoutBus(@RequestBody BusParam param){

        String busNo = param.getBusNumber();

        String curStop = param.getCurrentStop();

        String dir = param.getDirection();

        //调用helper的查询方法

        return helper.queryAction(busNo,curStop,dir);

    }

}

class BusParam{

    //这里BusParam的三个属性字段,需要前端与该字段保持一致,即post的key分别为这三个字段

    private String busNumber;//公交车号

    private String currentStop;//当前的站点

    private String direction;//方向

    public void setBusNumber(String busNO){

        this.busNumber = busNO;

    }

    public String getBusNumber(){

        return busNumber;

    }

    public void setCurrentStop(String curStop){

        this.currentStop = curStop;

    }

    public String getCurrentStop(){

        return currentStop;

    }

    public void setDirection(String dir){

        this.direction = dir;

    }

    public String getDirection(){

        return direction;

    }

}

//查询的类

class BusQueryHelper {

    //传入公交车号、当前站点、方向

    public Object queryAction(String busNO,String currentStop,String direction){

        Map map = new HashMap();

        //查询成功 模拟

        if (busNO.equals("706") && currentStop.equals("明珠路") && direction.equals("1")){

            map.put("result","1");

            map.put("nextStop","京华路");

            map.put("waitingTime","3分钟");

            map.put("restStopCount","5");

            List stopList = new ArrayList();

            stopList.add("京华路");

            stopList.add("华徐公路");

            stopList.add("蟠龙路");

            stopList.add("蟠秀路");

            stopList.add("徐泾东站");

            map.put("restStops",stopList);

            return map;

        }

        //查询失败

        map.put("result","0");

        map.put("message","未查询到班次");

        return map;

    }

}

前端代码(iOS):

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithRed:234/255.f green:234/255.f blue:234/255.f alpha:1.0];

    [self testQueryBus];

}

- (void)testQueryBus{

    NSMutableDictionary *param = [NSMutableDictionary dictionary];

    [param setObject:@"706" forKey:@"busNumber"];

    [param setObject:@"明珠路" forKey:@"currentStop"];

    [param setObject:@"1" forKey:@"direction"];

    [self requestPostWithParam:param withUrlString:@"[http://localhost:8080/api/queryBus](http://localhost:8080/api/queryBus)"];

}

- (void)requestPostWithParam:(NSDictionary*)param withUrlString:(NSString*)urlString

{

    //1 获取URL

    NSURL *url = [NSURL URLWithString:urlString];

    //构建请求参数

    //2 初始化manager

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    // manager的响应类型

    manager.responseSerializer = [AFJSONResponseSerializer serializer];

    manager.requestSerializer = [AFJSONRequestSerializer serializer];

    //3 使用AFNetWorking的GET方法进行网络请求

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        [manager POST:url.absoluteString parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {

        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

            if ([responseObject isKindOfClass:[NSDictionary class]]) {

                NSDictionary *dic = (NSDictionary*)responseObject;

                NSLog(@"dic内容:%@",dic);

            }

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        }];

    });

}

效果:

屏幕快照 2018-12-25 下午3.55.00.png

梳理图:

屏幕快照 2018-12-25 下午3.15.05.png

加油~

你可能感兴趣的:(SpringBoot之Post请求)