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 syntaxjava.io.StreamCorruptedException: invalid stream header: 68747470SerializationException: 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
| Symptom | Root Cause |
|---|---|
Invalid URI syntax: Illegal character in scheme name at index 0 | Quotation marks mistakenly included in the application.properties value |
StreamCorruptedException: invalid stream header: 68747470 | HTTP string (http) was interpreted as binary-serialized Java object |
SerializationException: could not deserialize | Incompatible 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:
68747470is 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
| Do | Don’t |
|---|---|
Use unquoted URI values in application.properties | Wrap URIs in double quotes |
Use String in entities and convert to URI in service code | Store URI or complex types directly in JPA entities |
Use @ConfigurationProperties for structured configuration | Hard-code URIs in the application |
Validate URIs with URI.create() or java.net.URL | Rely 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: 68747470IllegalArgumentException: Invalid URI syntaxSerializationException: could not deserialize
Make sure to:
- Avoid double quotes around values
- Use
Stringinstead of complex objects in your persistence layer - Convert types manually in code (e.g.,
URI.create(stringValue))


