我有一个 Java Spring Boot 错误@RequestParam("")

问题描述 投票:0回答:2

我是 Java 和 Spring Boot 新手,尝试通过 GET 发送 JSON 时遇到错误。

对于我使用 Postman 的测试,我收到此错误:

{
"status": "INTERNAL_SERVER_ERROR",
"message": "Required request parameter 'tags' for method parameter type List is not present"
}

JSON证明:

"tags": [
  "potable",
  "contaminada",
  "tratada"
]

各层非常简单:

控制器:

@GetMapping("/search")
public ResponseEntity<List<PropuestaEntity>> SearchTags(@RequestParam("tags") List<String> tag) {
    List<PropuestaEntity> propuesta = propuestaServices.SearchTags(tag);
    return ResponseEntity.ok(propuesta);
}

服务:

public List<PropuestaEntity> SearchTags(List<String> tag) {
    return propuestaRepository.findByTags(tag);
}

存储库:

public interface PropuestaRepository extends MongoRepository<PropuestaEntity, Integer> {
    List<PropuestaEntity> findByTags(List<String> tag);
}

mongobd 中的数据:

{
  "titulo": "Análisis de la calidad del agua en el río X sdauiyuadiossd",
  "tags": ["agua", "calidad", "contaminación", "metales pesados", "nutrientes"],
  "estadoPropuesta":1
}

主要目标是通过之前分配给提案的标签搜索提案(英文),并列出包含这些标签的所有提案。

java spring-boot spring-mvc http-request-parameters
2个回答
1
投票

我不太明白你所说的“JSON 来证明”是什么意思,但从格式来看,你似乎正在呈现一个请求负载。为了证明任何事情,您需要显示整个请求(包括完整的请求 URL 和负载)。

如果您通过请求正文发送

tags
,则需要更改控制器中的注释以在请求负载中查找
tags

@GetMapping("/search")
public ResponseEntity<List<PropuestaEntity>> SearchTags(@RequestBody List<String> tags) {
    List<PropuestaEntity> propuesta = propuestaServices.searchTags(tags);
    return ResponseEntity.ok(propuesta);
}

在这种情况下,您的有效负载应如下所示,以便它可以与控制器方法中的

List
类型匹配:

[
  "potable",
  "contaminada",
  "tratada"
]

如果您确实想使用

@RequestParam
注释,则必须将标签作为查询参数包含在 URL 中,如下所示:

https://your-service.com/search?tags=potable&tags=contaminada&tags=tratada

0
投票

这可能是您传递路径变量的方式。我尝试了一个简单的测试方法 -

@GetMapping("/search/{tags}")
public ResponseEntity<List<String>> SearchTags(@PathVariable(name="tags") List<String> tag) {
    List<String> value = new ArrayList<>(); 
    value.add(tag.get(0));
    return ResponseEntity.ok(value);
}

请求 - GET http://localhost:8080/search/potable,ontaminada

回应 - [ “饮用” ]

© www.soinside.com 2019 - 2024. All rights reserved.