前言
语言都是相通的,只要搞清楚概念后就可以编写代码了。而概念是需要学习成本的。
本文首发于博客园-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;
}
}
- LOG: 采用Sl4J接口
- 参数:
@PathVariable
可以接收url路径中的参数 - 参数:
@RequestParam
可以接收?
后的query参数 - 响应:
@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人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦