java 接口参数处理

/*
 * Copyright 2018 tomoncle
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
  
 package com.tomoncle.app.restful.api.controller;
  
 import com.alibaba.fastjson.JSONObject;
 import lombok.Data;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
  
 import java.util.HashMap;
  
 /**
 * @author tomoncle
 */
 @RestController
 @RequestMapping("/rest/post")
 public class PostController {
 private static Logger logger = LoggerFactory.getLogger(PostController.class);
  
 /**
 * curl -i -X POST \
 * -H "Content-Type:application/x-www-form-urlencoded" \
 * -d \
 * '{"data":[{"sourcePort":12982,"sourceIp":"119.12.15.55","url":"http://www.baidu.com/abc.html","timestamp":1589445557967}]}' \
 * 'http://localhost:8910/rest/post-form-urlencoded'
 *
 * @param map json data
 * @return JSONObject
 */
 @PostMapping(value = "/form-urlencoded", headers = {"Content-Type=application/x-www-form-urlencoded"})
 public JSONObject anonymous(@RequestParam HashMap map) {
 if (map.keySet().isEmpty()) {
 return new JSONObject();
 }
 String jsonData = map.keySet().iterator().next().toString();
 logger.debug("form-urlencoded anonymous: " + jsonData);
 return JSONObject.parseObject(jsonData);
 }
  
 /**
 * curl -X POST --header "Content-Type: application/x-www-form-urlencoded" \
 * "http://localhost:8910/rest/post-form-urlencoded?map=%7B%22data%22%3A%5B%7B%22sourcePort%22%3A12982%2C%22sourceIp%22%3A%22119.12.15.55%22%2C%22url%22%3A%22http%3A%2F%2Fwww.baidu.com%2Fabc.html%22%2C%22timestamp%22%3A1589445557967%7D%5D%7D"
 *
 * @param map json data
 * @return JSONObject
 */
 @PostMapping(value = "/form-urlencoded", params = {"map"}, headers = {"Content-Type=application/x-www-form-urlencoded"})
 public JSONObject specify(@RequestParam HashMap map) {
 String jsonData = map.get("map").toString();
 logger.debug("form-urlencoded specify: " + jsonData);
 return JSONObject.parseObject(jsonData);
 }
  
 /**
 * curl -i -X POST \
 * -H "Content-Type:multipart/form-data" \
 * -F "file=@\"./狗狗连喊带比划叫小伙伴出门玩.mp4\";type=video/mp4;filename=\"狗狗连喊带比划叫小伙伴出门玩.mp4\"" \
 * 'http://localhost:8910/rest/post/form-data'
 *
 * @param file file
 * @return json
 */
 @PostMapping(value = "/form-data", headers = {"Content-Type=multipart/form-data"})
 public JSONObject formDataAnonymous(@RequestParam("file") MultipartFile file) {
 logger.debug("form-data anonymous: " + file.getOriginalFilename());
 return new JSONObject(new HashMap<String, Object>() {{
 put("file", file.getOriginalFilename());
 }});
 }
  
 /**
 * curl -i -X POST \
 * -H "Content-Type:multipart/form-data" \
 * -F "username=tomoncle" \
 * -F "password=123456" \
 * -F "file=@\"./狗狗连喊带比划叫小伙伴出门玩.mp4\";type=video/mp4;filename=\"狗狗连喊带比划叫小伙伴出门玩.mp4\"" \
 * 'http://localhost:8910/rest/post/form-data'
 *
 * @param file file
 * @param username string
 * @param password string
 * @return json
 */
 @PostMapping(value = "/form-data", params = {"username", "password"}, headers = {"Content-Type=multipart/form-data"})
 public JSONObject formDataAnonymous(@RequestParam("file") MultipartFile file,
 @RequestParam String username,
 @RequestParam String password) {
 logger.debug("form-data anonymous: " + file.getOriginalFilename() + username + password);
 // 测试使用,禁止生产环境这样写
 return new JSONObject(new HashMap<String, Object>() {{
 put("file", file.getOriginalFilename());
 put("username", username);
 put("password", password);
 }});
 }
  
 /**
 * curl -i -X POST \
 * -H "Content-Type:multipart/form-data" \
 * -F "username=12" \
 * -F "age=12" \
 * 'http://localhost:8910/rest/post/form-data-object'
 *
 * @param map 匿名hash参数,传入key=value
 * @return json
 */
 @PostMapping(value = "/form-data-map", headers = {"Content-Type=multipart/form-data"})
 public JSONObject formDataAnonymous(@RequestParam HashMap map) {
 logger.debug("form-data-map anonymous: " + map);
 return (JSONObject) JSONObject.toJSON(map);
 }
  
 /**
 * curl -i -X POST \
 * -H "Content-Type:application/json" \
 * -d \
 * '{"data":[{"sourcePort":12982,"sourceIp":"119.12.15.55","url":"http://www.baidu.com/abc.html","timestamp":1589445557967}]}' \
 * 'http://localhost:8910/rest/post/body-json'
 *
 * @param jsonObject json
 * @return json
 */
 @PostMapping(value = "/body-json", headers = {"Content-Type=application/json"})
 public JSONObject anonymous(@RequestBody JSONObject jsonObject) {
 logger.debug("body-json anonymous: " + jsonObject);
 return jsonObject;
 }
  
  
 @Data
 public static class UserForm {
 private String username;
 private String age;
 private String password;
 private MultipartFile file;
 }
  
 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值