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

Spring接口SocketTimeoutException求助:Mac用户大CSV请求失败

Troubleshooting SocketTimeoutException for Large Requests on MacOS Clients

Let's break down your issue and walk through actionable steps to fix it. The core problem here is that MacOS users are hitting a SocketTimeoutException when sending large CSV data in the data field (over 20 rows/500KB), even though local testing works fine. Here's what's likely going on and how to resolve it:

1. Check Spring Boot's Request Timeout & Payload Limits

The most common culprit for this kind of issue is restrictive default settings in Spring that can't handle larger request bodies, especially when combined with slower network speeds (common in non-local environments like user machines).

Fixes to Apply in application.properties/application.yml:

  • Increase connection/read timeout: Give enough time for large payloads to transfer over the network:
    # General connection timeout (adjust based on your needs, e.g., 30 seconds)
    server.servlet.connection-timeout=30s
    # Tomcat-specific timeout (if you're using Tomcat)
    server.tomcat.connection-timeout=30000
    # Disable or increase the maximum "swallowed" request size (prevents Tomcat from truncating large bodies)
    server.tomcat.max-swallow-size=-1
    
  • Raise maximum request body size: Spring has default limits on how large a request can be. Update these to accommodate your CSV data:
    spring.servlet.multipart.max-request-size=10MB
    spring.servlet.multipart.max-file-size=10MB
    # For non-multipart requests, adjust header/body size limits
    server.max-http-request-header-size=10MB
    

2. Optimize Your API Code

Your current approach of manually parsing a String body with a new ObjectMapper per request is inefficient and can contribute to performance bottlenecks with large payloads.

Refactor for Better Performance & Reliability:

  • Use Spring's built-in JSON binding instead of manual parsing:
    @RestController
    public class YourController {
        // Inject Spring's pre-configured ObjectMapper (singleton, efficient)
        private final ObjectMapper objectMapper;
    
        public YourController(ObjectMapper objectMapper) {
            this.objectMapper = objectMapper;
        }
    
        @PostMapping("/abc") 
        public ResponseEntity createObject(@RequestBody ClassA abc) throws Exception{
            // Spring automatically parses the JSON into your ClassA object
            // ... your business logic here
        }
    }
    
  • This eliminates redundant ObjectMapper initialization and leverages Spring's optimized message converters, which handle large payloads more reliably.

3. Rule Out MacOS-Specific Network Issues

MacOS has some default network configurations or third-party tools that can interfere with large request transfers:

  • Check for proxies/VPNs: Tools like Charles Proxy, VPN clients, or even corporate firewalls can throttle or delay large requests. Ask users to disable these temporarily and retry.
  • Test with raw curl: Have users run a direct curl command to rule out client-side issues (e.g., browser/app timeout settings):
    curl -X POST -H "Content-Type: application/json" -d @large-request.json http://your-api-domain/abc
    
  • Verify client-side timeouts: If users are using a custom client (like Axios, fetch, or a desktop app), ensure its timeout setting is longer than your server's timeout (e.g., 40 seconds to account for network latency).

4. Add Detailed Logging to Narrow Down the Issue

To confirm whether the timeout happens during request transfer or JSON parsing, add logging to track payload size and parsing time:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.charset.StandardCharsets;

@RestController
public class YourController {
    private static final Logger log = LoggerFactory.getLogger(YourController.class);

    @PostMapping("/abc") 
    public ResponseEntity createObject(@RequestBody String jsonStr) throws Exception{
        // Log payload size to confirm it's reaching the server
        int payloadSize = jsonStr.getBytes(StandardCharsets.UTF_8).length;
        log.info("Received request with payload size: {} bytes", payloadSize);
        
        // Track parsing time
        long parseStart = System.currentTimeMillis();
        ObjectMapper objectMapper = new ObjectMapper(); 
        ClassA abc = objectMapper.readValue(jsonStr, ClassA.class); 
        log.info("Parsed JSON to ClassA in {} ms", System.currentTimeMillis() - parseStart);
        
        // ... rest of your logic
    }
}

If the payload size never logs, the timeout is happening before the request reaches your controller. If parsing takes longer than your timeout, you'll need to optimize parsing or increase the timeout further.

Final Steps to Test

  1. Apply the Spring configuration changes first (timeout and payload limits) — this fixes 90% of similar issues.
  2. Refactor your code to use Spring's automatic JSON binding for better performance.
  3. Have MacOS users test with curl to isolate client vs. server issues.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 21:28:11