Fix StreamCorruptedException and Invalid URI Errors in application.properties in Spring Boot

When configuring a Spring Boot application, developers often use application.properties or application.yml files to define endpoints, tokens, and other environment-specific values. However, if not handled correctly, these configurations can lead to confusing errors such as:

  • java.lang.IllegalArgumentException: Invalid URI syntax
  • java.io.StreamCorruptedException: invalid stream header: 68747470
  • SerializationException: could not deserialize

In this article, we’ll explore why these errors happen and how to fix them, especially when working with URIs or other complex values.


๐Ÿ’ก Root Cause Summary

SymptomRoot Cause
Invalid URI syntax: Illegal character in scheme name at index 0Quotation marks mistakenly included in the application.properties value
StreamCorruptedException: invalid stream header: 68747470HTTP string (http) was interpreted as binary-serialized Java object
SerializationException: could not deserializeIncompatible type mapping or deserialization logic in JPA or Hibernate

โœ… Correct Way to Configure URIs in application.properties

โŒ Incorrect: Quoted String

api.endpoint="https://example.com/api/token"

This will include the quotes in the value ("https://example.com/api/token"), which is invalid for URI or URL types.

โœ… Correct: Unquoted

api.endpoint=https://example.com/api/token

No quotes needed. Spring Boot will properly parse this as a String or convert it to a java.net.URI if the field expects it.


๐Ÿ› ๏ธ Sample Configuration and Bean Injection

Let’s define a configuration property class.

โœ… Java Configuration Class

@Configuration
@ConfigurationProperties(prefix = "api")
public class ApiProperties {

    private URI endpoint;

    public URI getEndpoint() {
        return endpoint;
    }

    public void setEndpoint(URI endpoint) {
        this.endpoint = endpoint;
    }
}

โœ… Bean Usage

@Service
public class ApiService {
    
    private final ApiProperties apiProperties;

    public ApiService(ApiProperties apiProperties) {
        this.apiProperties = apiProperties;
    }

    public void callApi() {
        URI uri = apiProperties.getEndpoint();
        System.out.println("Calling API at " + uri);
        // Use RestTemplate or WebClient here...
    }
}

โœ… Corresponding application.properties

api.endpoint=https://example.com/api/token

๐Ÿงจ What Happens If You Add Quotes?

โŒ With Quotes in application.properties

api.endpoint="https://example.com/api/token"

โŒ Results in Error

Caused by: java.lang.IllegalArgumentException: Invalid URI syntax: "https://example.com/api/token"

This is because the URI now includes the quote characters ("), which are invalid.


โš ๏ธ What Happens If You Use URI as Serializable?

In some cases (e.g., JPA entities), if you’re trying to persist a URI field, and the framework attempts to serialize it using Java’s default Serializable mechanism, but you accidentally feed a non-binary string (like an HTTP URL), you’ll get:

StreamCorruptedException: invalid stream header: 68747470

This is because:

  • 68747470 is the hexadecimal ASCII of 'http'
  • Java expects a serialized object stream header (AC ED 00 05 ...) for deserialization

โŒ Common Mistake: Mapping a URI to a Serialized Field

@Entity
public class Bank {

    @Id
    private Long id;

    @Lob
    private URI tokenEndpoint; // โš ๏ธ Will try to serialize/deserialize this

    // Getter/Setter
}

This would require Java object serialization into a BLOB/CLOB, which is not ideal for a URI.

โœ… Solution: Store as String

@Entity
public class Bank {

    @Id
    private Long id;

    private String tokenEndpoint; // โœ… Store URI as string

    public URI getTokenEndpointAsUri() {
        return URI.create(tokenEndpoint);
    }

    // Getter/Setter
}

โœ… Best Practices for URI Configuration in Spring Boot

DoDon’t
Use unquoted URI values in application.propertiesWrap URIs in double quotes
Use String in entities and convert to URI in service codeStore URI or complex types directly in JPA entities
Use @ConfigurationProperties for structured configurationHard-code URIs in the application
Validate URIs with URI.create() or java.net.URLRely blindly on deserialization for externalized config

๐Ÿ” Final Thoughts

When configuring Spring Boot apps, understanding the way Java parses application.properties is crucial. Misconfigured values can cause misleading errors, especially when using complex types like URI, Map, or List.

To avoid issues like:

  • invalid stream header: 68747470
  • IllegalArgumentException: Invalid URI syntax
  • SerializationException: could not deserialize

Make sure to:

  • Avoid double quotes around values
  • Use String instead of complex objects in your persistence layer
  • Convert types manually in code (e.g., URI.create(stringValue))
This article is inspired by real-world challenges we tackle in our projects. If you're looking for expert solutions or need a team to bring your idea to life,

Let's talk!

    Please fill your details, and we will contact you back

      Please fill your details, and we will contact you back