Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update - Migrate from Zalando to Spring Framework 6 problem support implementation #1024

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions jhipster-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@
<neo4j-migrations.version>1.13.1</neo4j-migrations.version>
<mysql-jdbc.version>8.0.30</mysql-jdbc.version>
<oracle-jdbc.version>21.7.0.0</oracle-jdbc.version>
<problem-spring.version>0.27.0</problem-spring.version>
<problem-spring-web.version>${problem-spring.version}</problem-spring-web.version>
<problem-spring-webflux.version>${problem-spring.version}</problem-spring-webflux.version>
<prometheus-simpleclient.version>0.15.0</prometheus-simpleclient.version>
<redisson.version>3.17.7</redisson.version>
<reflections.version>0.10.1</reflections.version>
<spring-cloud.version>2022.0.0-M5</spring-cloud.version>
Expand Down Expand Up @@ -256,16 +254,6 @@
<artifactId>ojdbc8</artifactId>
<version>${oracle-jdbc.version}</version>
</dependency>
<dependency>
<groupId>org.zalando</groupId>
<artifactId>problem-spring-web</artifactId>
<version>${problem-spring-web.version}</version>
</dependency>
<dependency>
<groupId>org.zalando</groupId>
<artifactId>problem-spring-webflux</artifactId>
<version>${problem-spring-webflux.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ public interface ExceptionTranslation {
* @param request The request that is being served
* @return Returns the Responseentity containing the ProblemDetail
*/
public Mono<ResponseEntity<ProblemDetail>> handleAnyException(Throwable ex, ServerWebExchange request);
public Mono<ResponseEntity<Object>> handleAnyException(Throwable ex, ServerWebExchange request);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright 2016-2022 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package tech.jhipster.web.rest.errors;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import org.springframework.http.ProblemDetail;

/*
* Class that extends Spring's ProblemDetail and has a Builder implementation.
*/
public class ProblemDetailWithCause extends ProblemDetail {
private ProblemDetailWithCause cause;

ProblemDetailWithCause(int rawStatus) {
super(rawStatus);
}

ProblemDetailWithCause(int rawStatus, ProblemDetailWithCause cause) {
super(rawStatus);
this.cause = cause;
}

public ProblemDetailWithCause getCause() {
return cause;
}

public void setCause(ProblemDetailWithCause cause) {
this.cause = cause;
}

// The missing builder from Spring
public static class ProblemDetailWithCauseBuilder {
private static final URI BLANK_TYPE = URI.create("about:blank");
// From Springs Problem Detail
private URI type = BLANK_TYPE;
private String title;
private int status;
private String detail;
private URI instance;
private Map<String, Object> properties = new HashMap<>();
private ProblemDetailWithCause cause;

public static ProblemDetailWithCauseBuilder instance(){
return new ProblemDetailWithCauseBuilder();
}

public ProblemDetailWithCauseBuilder withType(URI type) {
this.type = type;
return this;
}

public ProblemDetailWithCauseBuilder withTitle(String title) {
this.title = title;
return this;
}

public ProblemDetailWithCauseBuilder withStatus(int status) {
this.status = status;
return this;
}

public ProblemDetailWithCauseBuilder withDetail(String detail) {
this.detail = detail;
return this;
}

public ProblemDetailWithCauseBuilder withInstance(URI instance) {
this.instance = instance;
return this;
}

public ProblemDetailWithCauseBuilder withCause(ProblemDetailWithCause cause) {
this.cause = cause;
return this;
}

public ProblemDetailWithCauseBuilder withProperties(Map<String, Object> properties) {
this.properties = properties;
return this;
}

public ProblemDetailWithCauseBuilder withProperty(String key, Object value) {
this.properties.put(key, value);
return this;
}

public ProblemDetailWithCause build() {
ProblemDetailWithCause cause = new ProblemDetailWithCause(this.status);
cause.setType(this.type);
cause.setTitle(this.title);
cause.setDetail(this.detail);
cause.setInstance(this.instance);
this.properties.forEach(cause::setProperty);
cause.setCause(this.cause);
return cause;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ public ReactiveWebExceptionHandler(ExceptionTranslation translator, ObjectMapper
@Override
public Mono<Void> handle(final ServerWebExchange exchange, final Throwable throwable) {
if (throwable instanceof ResponseStatusException) {
final Mono<ResponseEntity<ProblemDetail>> entityMono = translator.handleAnyException(throwable, exchange);
final Mono<ResponseEntity<Object>> entityMono = translator.handleAnyException(throwable, exchange);
return entityMono.flatMap(entity -> this.setHttpResponse(entity, exchange, mapper));
}
return Mono.error(throwable);
}

private Mono<Void> setHttpResponse(final ResponseEntity<ProblemDetail> entity, final ServerWebExchange exchange,
private Mono<Void> setHttpResponse(final ResponseEntity<Object> entity, final ServerWebExchange exchange,
final ObjectMapper mapper) {
final ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(entity.getStatusCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private static ProblemDetail getProblemDetail() {
private static class TestExceptionTranslation implements ExceptionTranslation {

@Override
public Mono<ResponseEntity<ProblemDetail>> handleAnyException(Throwable ex, ServerWebExchange request) {
public Mono<ResponseEntity<Object>> handleAnyException(Throwable ex, ServerWebExchange request) {
logger.trace("Stub ExceptionTranslation handleAnyException invoked");
ProblemDetail problem = getProblemDetail();
HttpHeaders header = new HttpHeaders();
Expand Down