Spring请求参数测试

与同事对接接口时,常常没有完整的定义传输格式,请求方式而经常导致口头交流,有时候又不能准确的区分之间的请求方式,以及接收的类型。故写此测试方法。

仅个人测试,也许存在错误,还望指正

ps:注释掉的是设想封装请求,但没有成功的(具体原因日后分析)

package online.controller;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import online.entity.UserDao;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.Map;

/**
 * Controller测试接收各个参数
 * 各种请求对应各种接收方式
 */
@Slf4j
@RestController
@RequestMapping("ParamsController")
public class ParamsController {
    /**
     * restful
     */
    @GetMapping("restful/{name}")
    public Object restful(@PathVariable("name") String name) {
        log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), name);
        return name;
    }

    @GetMapping("restful")
    public Object restful1(@PathParam("name") String name) {
        log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), name);
        return name;
    }


    /**
     * 对象
     *  http://localhost/ParamsController/entry?id=123&name=xiaomimg
     *  Body中fromData
     */
    @GetMapping("entry")
    public Object entry(UserDao userDao) {
        log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), JSON.toJSONString(userDao));
        return userDao;
    }

    /**
     * 对象
     * 基础类-post/get均可
     *  Body中 row
     */
    @RequestMapping("/entryBody")
    public Object entryBody(@RequestBody UserDao userDao) {
        log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), JSON.toJSONString(userDao));
        return userDao;
    }

    /**
     * List  失败,没有构造参数
     * ArrayList 没有注入进去
     */
//    @RequestMapping("/listTest")
//    public Object listTest(ArrayList<String> name) {
//        log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), JSON.toJSONString(name));
//        return name;
//    }

    /**
     * List  失败,没有构造参数
     * ArrayList 没有注入进去
     */
//    @RequestMapping("/listUserTest")
//    public Object listUserTest(@RequestBody ArrayList<UserDao> name) {
//        log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), JSON.toJSONString(name));
//        return name;
//    }

    /**
     * List  失败,没有构造参数
     * ArrayList 没有注入进去
     */
    @RequestMapping("/listBodyTest")
    public Object listBodyTest(@RequestBody() ArrayList<String> name) {
        log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), JSON.toJSONString(name));
        return name;
    }

    /**+
     * Map
     *  mapTest?name=1&name1=2&name11=3
     *  body中FromData
     */
    @RequestMapping("mapTest")
    public Object mapTest(@RequestParam Map<String, Object> map) {
        log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), JSON.toJSONString(map));
        return map;
    }

    /**
     * Map
     *  body中raw
     */
    @RequestMapping("mapBodyTest")
    public Object mapBodyTest(@RequestBody Map<String, Object> map) {
        log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), JSON.toJSONString(map));
        return map;
    }

    /**
     * String[][不通]
     *  /StringTest?str=1&str=2&str=3
     *  Body-fromData
     */
//    @RequestMapping("StringTest")
//    public Object StringTest(String[] str) {
//        log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), JSON.toJSONString(str));
//        return str;
//    }

    /**
     * String[][不通]
     *  Body-raw
     */
//    @RequestMapping("StringBodyTest")
//    public Object StringBodyTest(@RequestBody String[] str) {
//        log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), JSON.toJSONString(str));
//        return str;
//    }
    /**
     * Integer...[不通]
     */
//    @RequestMapping("integerBodyTest")
//    public Object integerBodyTest(@RequestBody Integer... str) {
//        log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), JSON.toJSONString(str));
//        return str;
//    }
    /**
     * Integer...
     *  /integerTest?str=1&str=2&str=3
     *  Body-fromData
     */
    @RequestMapping("integerTest")
    public Object integerTest(Integer... str) {
        log.error("method: {}, name= {}", Thread.currentThread().getStackTrace()[1].getMethodName(), JSON.toJSONString(str));
        return str;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用一些测试工具来模拟请求参数为MultipartFile的情况。以下是一个示例,使用Java的Spring框架进行测试: 1. 首先,创建一个测试类,例如TestControllerTest。 ```java @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc public class TestControllerTest { @Autowired private MockMvc mockMvc; @Test public void testUploadFile() throws Exception { MockMultipartFile file = new MockMultipartFile( "file", "test.txt", MediaType.TEXT_PLAIN_VALUE, "Hello, World!".getBytes() ); mockMvc.perform(MockMvcRequestBuilders.multipart("/upload") .file(file)) .andExpect(status().isOk()) .andExpect(content().string("File uploaded successfully.")); } } ``` 2. 在上面的示例中,我们使用MockMultipartFile类创建了一个名为"file"的MultipartFile对象,并将其添加到请求中。然后,使用MockMvc进行请求的模拟,并对结果进行断言。 3. 创建一个Controller,例如TestController,其中包含一个处理文件上传请求的方法。 ```java @RestController public class TestController { @PostMapping("/upload") public String uploadFile(@RequestParam("file") MultipartFile file) { // 处理文件上传逻辑 // 返回适当的响应消息 return "File uploaded successfully."; } } ``` 4. 现在,你可以运行TestControllerTest中的测试方法来测试上传文件的接口。该方法将模拟一个包含MultipartFile参数请求,并断言响应是否符合预期。 这是一个使用Spring框架进行MultipartFile参数测试的示例。你可以根据自己的需求和技术栈进行调整和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值