or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

configuration.mdcontrollers.mdembedded-server.mderror-handling.mdhttp-clients.mdindex.mdjson-processing.mdstatic-resources.mdtesting.md

static-resources.mddocs/

0

# Static Resources

1

2

Serve static content like CSS, JavaScript, images, and other assets with built-in caching and versioning support.

3

4

## Capabilities

5

6

### Default Resource Locations

7

8

Configure where static resources are served from.

9

10

```properties { .api }

11

# Default static resource locations (in order of precedence)

12

spring.web.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

13

14

# Enable/disable default resource handling

15

spring.web.resources.add-mappings=true

16

17

# Static resource URL pattern

18

spring.mvc.static-path-pattern=/**

19

20

# WebJars URL pattern

21

spring.mvc.webjars-path-pattern=/webjars/**

22

```

23

24

### Resource Caching

25

26

Configure HTTP caching headers for static resources.

27

28

```properties { .api }

29

# Basic cache period (deprecated, use cache-control)

30

spring.web.resources.cache.period=3600

31

32

# Use last-modified headers

33

spring.web.resources.cache.use-last-modified=true

34

35

# Cache-Control header settings

36

spring.web.resources.cache.cachecontrol.max-age=3600

37

spring.web.resources.cache.cachecontrol.no-cache=false

38

spring.web.resources.cache.cachecontrol.no-store=false

39

spring.web.resources.cache.cachecontrol.must-revalidate=false

40

spring.web.resources.cache.cachecontrol.no-transform=false

41

spring.web.resources.cache.cachecontrol.cache-public=true

42

spring.web.resources.cache.cachecontrol.cache-private=false

43

spring.web.resources.cache.cachecontrol.proxy-revalidate=false

44

spring.web.resources.cache.cachecontrol.stale-while-revalidate=

45

spring.web.resources.cache.cachecontrol.stale-if-error=

46

spring.web.resources.cache.cachecontrol.s-max-age=

47

```

48

49

### Resource Chain Processing

50

51

Configure resource transformation and versioning strategies.

52

53

```properties { .api }

54

# Enable resource processing chain

55

spring.web.resources.chain.enabled=true

56

spring.web.resources.chain.cache=true

57

spring.web.resources.chain.compressed=false

58

59

# Content-based versioning (hash-based)

60

spring.web.resources.chain.strategy.content.enabled=true

61

spring.web.resources.chain.strategy.content.paths=/**

62

63

# Fixed versioning

64

spring.web.resources.chain.strategy.fixed.enabled=false

65

spring.web.resources.chain.strategy.fixed.paths=/**

66

spring.web.resources.chain.strategy.fixed.version=v1.0.0

67

```

68

69

### Programmatic Resource Configuration

70

71

Configure static resources programmatically.

72

73

```java { .api }

74

@Configuration

75

public class WebConfig implements WebMvcConfigurer {

76

77

/**

78

* Add custom resource handlers

79

*/

80

@Override

81

public void addResourceHandlers(ResourceHandlerRegistry registry) {

82

// Custom static content location

83

registry.addResourceHandler("/assets/**")

84

.addResourceLocations("classpath:/assets/", "file:./uploads/")

85

.setCachePeriod(3600)

86

.resourceChain(true)

87

.addResolver(new PathResourceResolver());

88

89

// Uploaded files

90

registry.addResourceHandler("/uploads/**")

91

.addResourceLocations("file:./uploads/")

92

.setCachePeriod(0); // No caching for user uploads

93

94

// Custom favicon

95

registry.addResourceHandler("/favicon.ico")

96

.addResourceLocations("classpath:/static/images/")

97

.setCachePeriod(86400); // 1 day cache

98

}

99

}

100

101

/**

102

* Resource handler registration

103

*/

104

public class ResourceHandlerRegistration {

105

public ResourceHandlerRegistration addResourceLocations(String... locations);

106

public ResourceHandlerRegistration setCachePeriod(Integer cachePeriod);

107

public ResourceHandlerRegistration setCacheControl(CacheControl cacheControl);

108

public ResourceChainRegistration resourceChain(boolean cacheResources);

109

public ResourceHandlerRegistration setOptimizeLocations(boolean optimizeLocations);

110

}

111

112

/**

113

* Resource chain configuration for transformations and resolvers

114

*/

115

public class ResourceChainRegistration {

116

public ResourceChainRegistration addResolver(ResourceResolver resolver);

117

public ResourceChainRegistration addTransformer(ResourceTransformer transformer);

118

}

119

```

120

121

### WebJar Support

122

123

Serve client-side libraries packaged as WebJars.

124

125

```java { .api }

126

/**

127

* WebJar configuration (automatically available when WebJars are on classpath)

128

*/

129

// Add WebJar dependency in Maven/Gradle:

130

// implementation 'org.webjars:bootstrap:5.1.3'

131

// implementation 'org.webjars:jquery:3.6.0'

132

133

// Access WebJar resources at:

134

// /webjars/bootstrap/5.1.3/css/bootstrap.min.css

135

// /webjars/jquery/3.6.0/jquery.min.js

136

137

/**

138

* Custom WebJar configuration

139

*/

140

@Configuration

141

public class WebJarConfig implements WebMvcConfigurer {

142

143

@Override

144

public void addResourceHandlers(ResourceHandlerRegistry registry) {

145

registry.addResourceHandler("/webjars/**")

146

.addResourceLocations("classpath:/META-INF/resources/webjars/")

147

.setCachePeriod(86400)

148

.resourceChain(true)

149

.addResolver(new WebJarsResourceResolver());

150

}

151

}

152

```

153

154

### Resource Resolvers and Transformers

155

156

Customize resource resolution and transformation.

157

158

```java { .api }

159

/**

160

* Built-in resource resolvers

161

*/

162

public class PathResourceResolver implements ResourceResolver {

163

// Resolves resources by matching request path to resource location

164

}

165

166

public class WebJarsResourceResolver implements ResourceResolver {

167

// Resolves WebJar resources with version-agnostic URLs

168

}

169

170

public class VersionResourceResolver implements ResourceResolver {

171

// Resolves versioned resources (content-based or fixed version)

172

}

173

174

/**

175

* Built-in resource transformers

176

*/

177

public class CssLinkResourceTransformer implements ResourceTransformer {

178

// Updates CSS @import and url() references

179

}

180

181

public class AppCacheManifestTransformer implements ResourceTransformer {

182

// Updates HTML5 AppCache manifest files

183

}

184

185

/**

186

* Custom resource resolver example

187

*/

188

@Component

189

public class CustomResourceResolver implements ResourceResolver {

190

191

@Override

192

public Resource resolveResource(HttpServletRequest request, String requestPath,

193

List<? extends Resource> locations, ResourceResolverChain chain) {

194

// Custom resource resolution logic

195

return chain.resolveResource(request, requestPath, locations);

196

}

197

198

@Override

199

public String resolveUrlPath(String resourcePath, List<? extends Resource> locations,

200

ResourceResolverChain chain) {

201

// Custom URL path resolution

202

return chain.resolveUrlPath(resourcePath, locations);

203

}

204

}

205

```

206

207

### Content Negotiation for Resources

208

209

Handle different resource formats based on request.

210

211

```java { .api }

212

@Configuration

213

public class ResourceConfig implements WebMvcConfigurer {

214

215

@Override

216

public void addResourceHandlers(ResourceHandlerRegistry registry) {

217

registry.addResourceHandler("/api/docs/**")

218

.addResourceLocations("classpath:/api-docs/")

219

.resourceChain(true)

220

.addResolver(new PathResourceResolver() {

221

@Override

222

protected Resource getResource(String resourcePath, Resource location) {

223

Resource resource = location.createRelative(resourcePath);

224

// Custom logic for format negotiation

225

if (resourcePath.endsWith(".json")) {

226

return resource.exists() ? resource : null;

227

}

228

return super.getResource(resourcePath, location);

229

}

230

});

231

}

232

}

233

```

234

235

## Types

236

237

```java { .api }

238

// Resource interface for accessing static resources

239

public interface Resource {

240

boolean exists();

241

String getFilename();

242

String getDescription();

243

InputStream getInputStream() throws IOException;

244

long contentLength() throws IOException;

245

long lastModified() throws IOException;

246

Resource createRelative(String relativePath) throws IOException;

247

}

248

249

// HTTP cache control configuration

250

public class CacheControl {

251

public static CacheControl maxAge(long maxAge, TimeUnit unit);

252

public static CacheControl noCache();

253

public static CacheControl noStore();

254

255

public CacheControl mustRevalidate();

256

public CacheControl cachePrivate();

257

public CacheControl cachePublic();

258

public CacheControl proxyRevalidate();

259

public CacheControl sMaxAge(long sMaxAge, TimeUnit unit);

260

}

261

262

// Resource handler registry for configuration

263

public class ResourceHandlerRegistry {

264

public ResourceHandlerRegistration addResourceHandler(String... pathPatterns);

265

public void setOrder(int order);

266

}

267

```