【问题标题】:RestTemplate GET with parameteres with Spring Boot Controller带有 Spring Boot 控制器的参数的 RestTemplate GET
【发布时间】:2019-11-19 10:08:57
【问题描述】:

我正在创建一个从 Rest Template 请求接收数据到 API 的微服务。我在 localhost:8080 运行微服务,在 localhost:13001 运行 API,并使用 Postman 对其进行测试。测试方法返回字符串。当请求有参数时会出现问题。使用下面的代码,我总是得到“404 Not Found”。但是当 API 控制器没有任何参数时 - 方法有效。无法解决这个问题。

顺序:

1) 邮递员 (GET http://localhost:8080/test)

2) 微服务​​控制器(Rest 模板交换)--> 代码如下

@RequestMapping(
        method                          = [RequestMethod.GET],
        produces                        = [MediaType.APPLICATION_JSON_VALUE],
        path                            = ["/test"],
        params                          = ["workspace_id"]
        )
@ResponseBody
fun test(
        authentication                  : OAuth2Authentication,
        @RequestParam(value             = "workspace_id")
        workspaceId                     : UUID
)                                       : String
{
    return serviceDashboard.get(authentication, workspaceId)
}

3) 休息模板 (GET http://localhost:13001/test?workspace_id=......) --> 下面的代码

    @Autowired
lateinit var restTemplate               : RestTemplate

    override fun get(
        authentication                  : OAuth2Authentication,
        workspaceId                     : UUID
)                                       : String
{
    //Header
    val token                           = (authentication.details as OAuth2AuthenticationDetails).tokenValue
    val headers                         = HttpHeaders()
    headers.setBearerAuth(token)
    val entity                          = HttpEntity<Any>(headers)
    //Parameters
    val params                          = HashMap<String, UUID>()
    params["workspace_id"]              = workspaceId
    //URI
    val endpoint                        = URI.create("http://localhost:13001/test")

    return restTemplate.exchange(endpoint.toString(), HttpMethod.GET, entity, String::class.java, params).body!!
}

4) API 控制器(返回数据)--> 代码如下

----------- 非工作版本----------------

@GetMapping(
        produces                        = [MediaType.APPLICATION_JSON_VALUE],
        params                          = ["workspace_id"],
        path                            = ["/test"])
@ResponseBody
fun test(
        @RequestParam(value = "workspace_id")
        workspaceId                     : UUID
)                                       : String
{
    if ( workspaceId.toString() == "650a539a-0356-467e-a0d0-71d472c41aae") return "It works"
    else return "It doesn't work"
}

-----------工作版本----

@GetMapping(
        produces                        = [MediaType.APPLICATION_JSON_VALUE]
        path                            = ["/test"])
@ResponseBody
fun test()                              : String
{
    return "It works"
}

【问题讨论】:

  • 您是否尝试过将@RequestParam(和/或@PathVariable)参数提供给函数而不使用 使用'params' 注释参数? (这就是我们一直在做的事情。)
  • 是的,像这样:@GetMapping(value="/test/{workspace_id}" 然后@PathVariable

标签: rest api spring-boot kotlin resttemplate


【解决方案1】:

您在类级别使用的注释是什么。 你需要使用@RestController。

【讨论】:

    猜你喜欢
    • 2020-11-09
    • 2012-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-01
    • 1970-01-01
    • 2021-08-12
    • 2018-06-08
    相关资源
    最近更新 更多