You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring中Query Parameters设置异常(逗号替代问号)问题修复咨询

Fixing Query Parameter Prefix Issue (Comma Instead of Question Mark) in Spring RequestEntity

Problem

I wrote this Java code to set up request headers and query parameters for a RequestEntity, but when running it, the query parameters in the request URI are prefixed with a comma (,) instead of the expected question mark (?). Testing with curl/Postman by replacing ? with , reproduces the same error.

Here's the problematic code:

public final RequestEntity<String> make(final Optional<String> cursor) {
    final HttpHeaders headers = new HttpHeaders();
    headers.add("Username", this.credentials.getUsername());
    headers.add("Password", this.credentials.getPassword());
    
    final UriComponentsBuilder body = UriComponentsBuilder.newInstance()
        .queryParam("record_limit", this.recordLimit.toString())
        .queryParam("cif", this.cif)
        .queryParam("account_type", this.accountType)
        .queryParam("account_status", this.accountStatus.getCode());
        
    cursor.ifPresent(cur -> body.queryParam("cursor", cur));
    
    return new RequestEntity<>(
        body.build().getQuery(), 
        headers, 
        HttpMethod.POST, 
        URI.create(this.url)
    );
}

Root Cause

The issue comes from a misunderstanding of how RequestEntity works here:

  • You're building query parameters with UriComponentsBuilder, but then extracting just the query string via getQuery() and passing it as the request body of the RequestEntity.
  • The URI you're passing (URI.create(this.url)) doesn't include any query parameters, so when the request is sent, the query string from the body is being incorrectly appended to the URI with a comma (this is likely a side effect of how the HTTP client handles combining the URI and unexpected body content for this request type).

Solution

You need to build the full URI (including query parameters) using UriComponentsBuilder, instead of passing the query string as the request body. Here's the fixed code:

public final RequestEntity<String> make(final Optional<String> cursor) {
    final HttpHeaders headers = new HttpHeaders();
    headers.add("Username", this.credentials.getUsername());
    headers.add("Password", this.credentials.getPassword());
    
    // Start with your base URL instead of a blank builder
    final UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(this.url)
        .queryParam("record_limit", this.recordLimit.toString())
        .queryParam("cif", this.cif)
        .queryParam("account_type", this.accountType)
        .queryParam("account_status", this.accountStatus.getCode());
        
    cursor.ifPresent(cur -> uriBuilder.queryParam("cursor", cur));
    
    // Build the full URI including query parameters (with ? prefix)
    URI fullUri = uriBuilder.build().toUri();
    
    // If your POST request doesn't require a body, pass null or an empty string
    return new RequestEntity<>(
        "", // Or null if no body is needed
        headers, 
        HttpMethod.POST, 
        fullUri
    );
}

Explanation

  1. Build full URI from base URL: Using UriComponentsBuilder.fromUriString(this.url) ensures we start with your base endpoint, then add query parameters directly to it. The builder will automatically handle the ? prefix for the first query parameter, and & for subsequent ones.
  2. Correct RequestEntity construction: The request body parameter is now an empty string (or null if your API doesn't expect any body content), while the full URI (with properly formatted query parameters) is passed as the target URI.
  3. Proper query parameter handling: The build().toUri() method generates a valid URI with the correct ? prefix and & separators between query parameters, eliminating the comma issue entirely.

内容的提问来源于stack exchange,提问作者iamjoshua

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 14:47:47