Java中controller层接Map_spring-mvc中controller,前端传过来的参数,用数组,列表,Map来接受参数的方式...
时间: 2024-03-21 10:39:40 浏览: 34
在Java的Spring MVC框架中,Controller层接收前端传递的参数可以使用以下方式:
1. 使用Map来接收参数:
```java
@RequestMapping("/test")
public String test(Map<String, Object> map) {
String username = (String)map.get("username");
// ...
return "test";
}
```
2. 使用@RequestParam注解来接收参数:
```java
@RequestMapping("/test")
public String test(@RequestParam("username") String username) {
// ...
return "test";
}
```
3. 使用数组来接收参数:
```java
@RequestMapping("/test")
public String test(String[] usernames) {
// ...
return "test";
}
```
4. 使用List或者Set来接收参数:
```java
@RequestMapping("/test")
public String test(List<String> usernames) {
// ...
return "test";
}
```
注意:以上示例中的参数名和前端传递的参数名必须一致,否则无法正确接收参数。
相关问题
写一个 单个参数 java后端 如何 用接受前端的json数据
可以使用 Spring MVC 框架中的 @RequestBody 注解来接收前端传来的 JSON 数据,具体步骤如下:
1. 在 Controller 中定义一个接收 JSON 数据的方法,方法的参数使用 @RequestBody 注解,如下所示:
```
@PostMapping("/api")
public void handleJsonRequest(@RequestBody Map<String, Object> json) {
// 处理接收到的 JSON 数据
}
```
2. 前端发送请求时,需要将数据转换成 JSON 格式,并设置请求头的 Content-Type 为 application/json,如下所示:
```
fetch('/api', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
// JSON 数据
})
})
```
3. 在后端处理接收到的 JSON 数据时,可以使用 Jackson 库将 JSON 数据转换成 Java 对象,如下所示:
```
ObjectMapper mapper = new ObjectMapper();
MyObject obj = mapper.convertValue(json, MyObject.class);
```
其中,MyObject 是一个 Java 类,用于表示接收到的 JSON 数据的结构。
获取controller中所有接口路径和名称
在Spring MVC中,可以通过扫描@Controller注解或@RequestMapping注解来获取Controller中所有的接口路径和名称。以下是一个获取@Controller中所有接口路径和名称的示例代码:
```java
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Controller
public class EndpointController {
private final RequestMappingHandlerMapping handlerMapping;
public EndpointController(RequestMappingHandlerMapping handlerMapping) {
this.handlerMapping = handlerMapping;
}
@RequestMapping("/endpoints")
public List<Endpoint> endpoints() {
List<Endpoint> endpoints = new ArrayList<>();
Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) {
RequestMappingInfo mapping = entry.getKey();
HandlerMethod method = entry.getValue();
Class<?> beanType = method.getBeanType();
if (AnnotationUtils.findAnnotation(beanType, Controller.class) != null) {
String name = method.getMethod().getName();
String path = mapping.getPatternsCondition().getPatterns().iterator().next();
endpoints.add(new Endpoint(name, path));
}
}
return endpoints;
}
public static class Endpoint {
private final String name;
private final String path;
public Endpoint(String name, String path) {
this.name = name;
this.path = path;
}
public String getName() {
return name;
}
public String getPath() {
return path;
}
}
}
```
在上面的代码中,我们注入了一个RequestMappingHandlerMapping实例,并且在endpoints()方法中遍历所有的RequestMappingInfo和HandlerMethod,找到所有被@Controller注解的方法,然后获取它们的方法名和路径,并返回一个Endpoint对象列表。最后,我们可以访问"/endpoints"接口来获取所有的接口路径和名称。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)