下面列出了org.springframework.http.ResponseEntity#BodyBuilder ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private final ResponseEntity.BodyBuilder newResponse(final HttpStatus status, final String etag)
{
ResponseEntity.BodyBuilder response = ResponseEntity.status(status)
.eTag(etag)
.cacheControl(CacheControl.maxAge(cacheMaxAgeSec, TimeUnit.SECONDS));
final String adLanguage = getAdLanguage();
if (adLanguage != null && !adLanguage.isEmpty())
{
final String contentLanguage = ADLanguageList.toHttpLanguageTag(adLanguage);
response.header(HttpHeaders.CONTENT_LANGUAGE, contentLanguage);
response.header(HttpHeaders.VARY, HttpHeaders.ACCEPT_LANGUAGE); // advice browser to include ACCEPT_LANGUAGE in their caching key
}
return response;
}
@ApiOperation("Update a platform")
@PutMapping("/{application_name}/platforms")
public ResponseEntity<PlatformIO> updatePlatform(Authentication authentication,
@PathVariable("application_name") final String applicationName,
@ApiParam(value = "Copie les propriétés du module déployé de la plateforme source avec l'ID correspondant. " +
"Si ce module ne contient pas de propriétés, à défaut on utilise les propriétés du module avec le même properties_path.")
@RequestParam(value = "copyPropertiesForUpgradedModules", required = false) final Boolean copyPropertiesForUpgradedModules,
@Valid @RequestBody final PlatformIO platformInput) {
Platform.Key platformKey = new Platform.Key(applicationName, platformInput.getPlatformName());
platformUseCases.updatePlatform(platformKey,
platformInput.toDomainInstance(),
Boolean.TRUE.equals(copyPropertiesForUpgradedModules), // on traite le cas `null`
new User(authentication)
);
final ResponseEntity.BodyBuilder response = ResponseEntity.status(HttpStatus.OK);
PlatformView platformView = platformUseCases.getPlatform(platformKey);
return response.body(new PlatformIO(platformView));
}
@Transactional(readOnly = true)
@Override
public ResponseEntity<StreamingResponseBody> download(String fileId) {
FileMeta fileMeta = getFileMeta(fileId);
ResponseEntity.BodyBuilder builder = ResponseEntity.ok();
builder.header(CONTENT_TYPE, fileMeta.getContentType());
builder.header(CONTENT_DISPOSITION, "attachment; filename=\"" + fileMeta.getFilename() + "\"");
Long contentLength = fileMeta.getSize();
if (contentLength != null) {
builder.contentLength(contentLength);
}
return builder.body(
outputStream -> {
try (ReadableByteChannel fromChannel = blobStore.newChannel(fileId)) {
ByteStreams.copy(fromChannel, Channels.newChannel(outputStream));
}
});
}
@GetMapping("/v1/sayHello")
public Mono<ResponseEntity<String>> sayHello() {
ResponseEntity.BodyBuilder unAuthenticated = ResponseEntity.status(HttpStatus.UNAUTHORIZED);
return ReactiveSecurityContext.getToken()
.doOnError(throwable -> Mono.just(unAuthenticated))
.map(token -> ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN)
.body(new Base64JwtDecoder().decode(token.getAppToken()).getPayload()));
}
/**
* We cache names if there are more than 3 services. This helps people getting started: if we
* cache empty results, users have more questions. We assume caching becomes a concern when zipkin
* is in active use, and active use usually implies more than 3 services.
*/
ResponseEntity<List<String>> maybeCacheNames(List<String> names) {
ResponseEntity.BodyBuilder response = ResponseEntity.ok();
if (serviceCount > 3) {
response.cacheControl(CacheControl.maxAge(namesMaxAge, TimeUnit.SECONDS).mustRevalidate());
}
return response.body(names);
}
@ExceptionHandler(HandledResponseEntityException.class)
public ResponseEntity<Error> handleResponseEntityException(HandledResponseEntityException e,
HttpServletRequest request) {
ResponseEntity.BodyBuilder bodyBuilder = ResponseEntity
.status(HttpStatus.PAYMENT_REQUIRED)
.header("custom-header", "custom-value");
if (e.getContentType() != null) {
bodyBuilder.contentType(e.getContentType());
}
return bodyBuilder.body(new Error(request.getRequestURI() + ":" + e.getMessage()));
}
public ResponseEntity<byte[]> toResponseEntity(@NonNull final ResponseEntity.BodyBuilder responseBuilder)
{
return responseBuilder
.contentType(MediaType.parseMediaType(getContentType()))
.header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + getImageName() + "\"")
.body(getImageData());
}
@Override
public ResponseEntity<InputStreamResource> getIcon(String definitionName, DefinitionImageType imageType) {
notNull(definitionName, "Definition name cannot be null.");
notNull(imageType, "Definition image type cannot be null.");
final Definition<?> definition = propertiesHelpers.getDefinition(definitionName);
notNull(definition, "Could not find definition of name %s", definitionName);
// Undefined and missing icon resources are simply 404.
String imagePath = definition.getImagePath(imageType);
if (imagePath == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
InputStream is = definition.getClass().getResourceAsStream(imagePath);
if (is == null) {
log.info("The image type %s should exist for %s at %s, but is missing. "
+ "The component should provide this resource.", imageType, definitionName, imagePath);
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
// At this point, we have enough information for a correct response.
ResponseEntity.BodyBuilder response = ResponseEntity.ok();
// Add the content type if it can be determined.
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
String contentType = mimeTypesMap.getContentType(imagePath);
if (contentType != null) {
response = response.contentType(MediaType.parseMediaType(contentType));
}
return response.body(new InputStreamResource(is));
}
private static ResponseEntity.BodyBuilder buildStatus ( int status ) {
return ResponseEntity.status( status );
}