為了賬號安全,請及時綁定郵箱和手機立即綁定

Java Web基礎入門,Springboot入門(4)

2017.12.07 19:41 1411瀏覽

前言

語言都是相通的,只要搞清楚概念后就可以編寫代碼了。而概念是需要學習成本的。

本文首發于博客園-Ryan Miao. 由于限制2000字,只能分多篇。

接收參數,響應JSON

新建一個controller, com.test.demo.controller.ParamController

package com.test.demo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Ryan on 2017/11/16/0016.
 */
@RestController
@RequestMapping("/param")
public class ParamController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ParamController.class);

    @GetMapping("/hotels/{htid}/rooms")
    public List<Long> getRooms(
            @PathVariable String htid,
            @RequestParam String langId,
            @RequestParam(value = "limit", required = false, defaultValue = "10") int limit,
            @RequestParam(value = "offset", required = false, defaultValue = "1") int offset
    ){
        final Map<String, Object> params = new HashMap<>();
        params.put("hotelId", htid);
        params.put("langId", langId);
        params.put("limit", limit);
        params.put("offset", offset);

        LOGGER.info("The params is {}", params);

        List<Long> roomIds = new ArrayList<>();
        roomIds.add(1L);
        roomIds.add(2L);
        roomIds.add(3L);

        return roomIds;
    }
}
  1. LOG: 采用Sl4J接口
  2. 參數: @PathVariable 可以接收url路徑中的參數
  3. 參數: @RequestParam 可以接收?后的query參數
  4. 響應: @RestController == @Controller+@ResponseBody, 其實,@ResponseBody注解表明這個方法會返回json,會將Java類轉換成JSON字符串,默認轉換器為Jackason

參數為JSON

新建class com.test.demo.entity.Room

public class Room {
    private Integer roomId;
    private String roomName;
    private String comment;

    public Integer getRoomId() {
        return roomId;
    }

    public void setRoomId(Integer roomId) {
        this.roomId = roomId;
    }

    public String getRoomName() {
        return roomName;
    }

    public void setRoomName(String roomName) {
        this.roomName = roomName;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }
}

假設,我們需要保存一個Room信息,先來get一個

@GetMapping("/hotels/{htid}/rooms/{roomId}")
public Room getRoomById(
        @PathVariable String htid,
        @PathVariable Integer roomId
){

    if (htid.equals("6606")){
        final Room room = new Room();
        room.setComment("None");
        room.setRoomId(roomId);
        room.setRoomName("豪華雙人間");

        return room;
    }

    return null;
}

然后保存一個

@PostMapping("/hotels/{htid}/rooms")
public Integer addRoom(@RequestBody Room room){
    final Random random = new Random();
    final int id = random.nextInt(10);
    room.setRoomId(id);

    LOGGER.info("Add a room: {}", room);

    return id;
}

接收數組參數

@GetMapping("/hotels/{htid}/rooms/ids")
public String getRoomsWithIds(@RequestParam List<Integer> ids){
    String s = ids.toString();
    LOGGER.info(s);
    return s;
}

瀏覽器訪問 http://localhost:8081/param//hotels/6606/rooms/ids?ids=1,2,3

下一篇, 參數校驗。

點擊查看更多內容

本文原創發布于慕課網 ,轉載請注明出處,謝謝合作

3人點贊

若覺得本文不錯,就分享一下吧!

評論

相關文章推薦

正在加載中
意見反饋 幫助中心 APP下載
官方微信

舉報

0/150
提交
取消
lpl竞猜