Skip to content

Commit 26aff45

Browse files
committed
cleanup work
1 parent e624cd2 commit 26aff45

15 files changed

Lines changed: 164 additions & 329 deletions

File tree

‎m4-lesson1/common/src/main/java/com/baeldung/common/web/RestResponseEntityExceptionHandler.java‎

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import javax.persistence.EntityNotFoundException;
66
import javax.validation.ConstraintViolationException;
77

8-
import org.slf4j.Logger;
9-
import org.slf4j.LoggerFactory;
108
import org.springframework.dao.DataAccessException;
119
import org.springframework.dao.DataIntegrityViolationException;
1210
import org.springframework.dao.InvalidDataAccessApiUsageException;
@@ -31,30 +29,23 @@
3129
import com.baeldung.common.web.exception.ValidationErrorDTO;
3230

3331
@ControllerAdvice
34-
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
35-
private Logger log = LoggerFactory.getLogger(getClass());
36-
37-
public RestResponseEntityExceptionHandler() {
38-
super();
39-
}
40-
41-
// API
32+
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
4233

4334
// 400
4435

4536
@Override
4637
protected final ResponseEntity<Object> handleHttpMessageNotReadable(final HttpMessageNotReadableException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
47-
log.info("Bad Request: {}", ex.getMessage());
48-
log.debug("Bad Request: ", ex);
38+
logger.info("Bad Request: ", ex);
39+
logger.debug("Bad Request: ", ex);
4940

5041
final ApiError apiError = message(HttpStatus.BAD_REQUEST, ex);
5142
return handleExceptionInternal(ex, apiError, headers, HttpStatus.BAD_REQUEST, request);
5243
}
5344

5445
@Override
5546
protected final ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
56-
log.info("Bad Request: {}", ex.getMessage());
57-
log.debug("Bad Request: ", ex);
47+
logger.info("Bad Request: ", ex);
48+
logger.debug("Bad Request: ", ex);
5849

5950
final BindingResult result = ex.getBindingResult();
6051
final List<FieldError> fieldErrors = result.getFieldErrors();
@@ -64,9 +55,9 @@ protected final ResponseEntity<Object> handleMethodArgumentNotValid(final Method
6455
}
6556

6657
@ExceptionHandler(value = { ConstraintViolationException.class, DataIntegrityViolationException.class })
67-
public final ResponseEntity<Object> handleBadRequest(final RuntimeException ex, final WebRequest request) {
68-
log.info("Bad Request: {}", ex.getLocalizedMessage());
69-
log.debug("Bad Request: ", ex);
58+
protected final ResponseEntity<Object> handleBadRequest(final RuntimeException ex, final WebRequest request) {
59+
logger.info("Bad Request: ", ex);
60+
logger.debug("Bad Request: ", ex);
7061

7162
final ApiError apiError = message(HttpStatus.BAD_REQUEST, ex);
7263
return handleExceptionInternal(ex, apiError, new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
@@ -76,7 +67,7 @@ public final ResponseEntity<Object> handleBadRequest(final RuntimeException ex,
7667

7768
@ExceptionHandler({ EntityNotFoundException.class, MyEntityNotFoundException.class, MyResourceNotFoundException.class })
7869
protected ResponseEntity<Object> handleNotFound(final RuntimeException ex, final WebRequest request) {
79-
log.warn("Not Found: {}", ex.getMessage());
70+
logger.warn("Not Found: ", ex);
8071

8172
final ApiError apiError = message(HttpStatus.NOT_FOUND, ex);
8273
return handleExceptionInternal(ex, apiError, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
@@ -86,7 +77,7 @@ protected ResponseEntity<Object> handleNotFound(final RuntimeException ex, final
8677

8778
@ExceptionHandler({ InvalidDataAccessApiUsageException.class, DataAccessException.class, MyConflictException.class })
8879
protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final WebRequest request) {
89-
log.warn("Conflict: {}", ex.getMessage());
80+
logger.warn("Conflict: ", ex);
9081

9182
final ApiError apiError = message(HttpStatus.CONFLICT, ex);
9283
return handleExceptionInternal(ex, apiError, new HttpHeaders(), HttpStatus.CONFLICT, request);
@@ -96,7 +87,7 @@ protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final
9687

9788
@ExceptionHandler({ InvalidMimeTypeException.class, InvalidMediaTypeException.class })
9889
protected ResponseEntity<Object> handleInvalidMimeTypeException(final IllegalArgumentException ex, final WebRequest request) {
99-
log.warn("Unsupported Media Type: {}", ex.getMessage());
90+
logger.warn("Unsupported Media Type: ", ex);
10091

10192
final ApiError apiError = message(HttpStatus.UNSUPPORTED_MEDIA_TYPE, ex);
10293
return handleExceptionInternal(ex, apiError, new HttpHeaders(), HttpStatus.UNSUPPORTED_MEDIA_TYPE, request);
@@ -105,7 +96,7 @@ protected ResponseEntity<Object> handleInvalidMimeTypeException(final IllegalArg
10596
// 500
10697

10798
@ExceptionHandler({ NullPointerException.class, IllegalArgumentException.class, IllegalStateException.class })
108-
public ResponseEntity<Object> handle500s(final RuntimeException ex, final WebRequest request) {
99+
protected ResponseEntity<Object> handle500s(final RuntimeException ex, final WebRequest request) {
109100
logger.error("500 Status Code", ex);
110101

111102
final ApiError apiError = message(HttpStatus.INTERNAL_SERVER_ERROR, ex);

‎m4-lesson1/common/src/main/java/com/baeldung/common/web/exception/ApiError.java‎

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ public class ApiError {
66
private String message;
77
private String developerMessage;
88

9-
public ApiError(final int status, final String message, final String developerMessage) {
10-
super();
11-
9+
public ApiError(final int status, final String message, final String developerMessage) {
1210
this.status = status;
1311
this.message = message;
1412
this.developerMessage = developerMessage;
@@ -40,46 +38,7 @@ public void setDeveloperMessage(final String developerMessage) {
4038
this.developerMessage = developerMessage;
4139
}
4240

43-
//
44-
45-
@Override
46-
public final int hashCode() {
47-
final int prime = 31;
48-
int result = 1;
49-
result = prime * result + ((developerMessage == null) ? 0 : developerMessage.hashCode());
50-
result = prime * result + ((message == null) ? 0 : message.hashCode());
51-
result = prime * result + status;
52-
return result;
53-
}
54-
55-
@Override
56-
public final boolean equals(final Object obj) {
57-
if (this == obj) {
58-
return true;
59-
}
60-
if (obj == null) {
61-
return false;
62-
}
63-
if (getClass() != obj.getClass()) {
64-
return false;
65-
}
66-
final ApiError other = (ApiError) obj;
67-
if (developerMessage == null) {
68-
if (other.developerMessage != null) {
69-
return false;
70-
}
71-
} else if (!developerMessage.equals(other.developerMessage)) {
72-
return false;
73-
}
74-
if (message == null) {
75-
if (other.message != null) {
76-
return false;
77-
}
78-
} else if (!message.equals(other.message)) {
79-
return false;
80-
}
81-
return status == other.status;
82-
}
41+
//
8342

8443
@Override
8544
public final String toString() {

‎m4-lesson1/pom.xml‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parent>
1515
<groupId>org.springframework.boot</groupId>
1616
<artifactId>spring-boot-starter-parent</artifactId>
17-
<version>1.4.0.RELEASE</version>
17+
<version>1.4.3.RELEASE</version>
1818
<relativePath /> <!-- lookup parent from repository -->
1919
</parent>
2020

@@ -91,14 +91,12 @@
9191
<java.version>1.8</java.version>
9292

9393
<!-- overriding Spring Boot -->
94-
<spring-hateoas.version>0.21.0.RELEASE</spring-hateoas.version>
95-
<spring-boot.version>1.4.0.RELEASE</spring-boot.version>
94+
<spring-hateoas.version>0.21.0.RELEASE</spring-hateoas.version>
9695

9796
<commons-collections.version>3.2.2</commons-collections.version>
9897
<hibernate.version>5.2.1.Final</hibernate.version>
9998
<hibernate-validator.version>5.2.4.Final</hibernate-validator.version>
100-
<httpclient.version>4.5.2</httpclient.version>
101-
<jackson.version>2.8.1</jackson.version>
99+
<httpclient.version>4.5.2</httpclient.version>
102100
<javassist.version>3.20.0-GA</javassist.version>
103101
<mysql.version>5.1.39</mysql.version>
104102

@@ -112,6 +110,7 @@
112110

113111
<!-- marshalling -->
114112
<jaxb-api.version>2.2.12</jaxb-api.version>
113+
<jackson.version>2.8.6</jackson.version>
115114

116115
<!-- commons and utils -->
117116
<commons-lang3.version>3.4</commons-lang3.version>

‎m4-lesson1/um-webapp/src/main/java/com/baeldung/um/persistence/setup/MyApplicationContextInitializer.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ public void initialize(final ConfigurableApplicationContext applicationContext)
3434
environment.getPropertySources().addFirst(new ResourcePropertySource("classpath:env-" + envTarget + ".properties"));
3535

3636
final String activeProfiles = environment.getProperty("spring.profiles.active");
37-
environment.setActiveProfiles(activeProfiles.split(","));
37+
if (activeProfiles != null) {
38+
environment.setActiveProfiles(activeProfiles.split(","));
39+
}
3840
} catch (final IOException ioEx) {
3941
if (envTarget != null) {
4042
logger.warn("Didn't find env-" + envTarget + ".properties in classpath so not loading it in the AppContextInitialized", ioEx);

‎m4-lesson1/um-webapp/src/main/java/com/baeldung/um/service/StartupLoggingComponent.java‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ public void afterPropertiesSet() {
3535
try {
3636
logEnvTarget(env);
3737
logPersistenceTarget(env);
38-
39-
logActiveSpringProfile(env);
38+
4039
logPersistenceData(env);
4140
} catch (final Exception ex) {
4241
logger.warn("There was a problem logging data on startup", ex);
@@ -55,12 +54,7 @@ private void logEnvTarget(final Environment environment) {
5554
private void logPersistenceTarget(final Environment environment) {
5655
final String envTarget = getValueOfProperty(environment, PERSISTENCE_TARGET_KEY, "h2", Lists.newArrayList("h2", "mysql", "cargo"));
5756
logger.info("{} = {}", PERSISTENCE_TARGET_KEY, envTarget);
58-
}
59-
60-
private void logActiveSpringProfile(final Environment environment) {
61-
final String activeSpringProfile = getValueOfProperty(environment, ACTIVE_SPRING_PROFILE_KEY, "none", null);
62-
logger.info("{} = {}", ACTIVE_SPRING_PROFILE_KEY, activeSpringProfile);
63-
}
57+
}
6458

6559
private void logPersistenceData(final Environment environment) {
6660
final String persistenceHost = getValueOfProperty(environment, PERSISTENCE_HOST_KEY, "not-found", null);

‎m4-lesson2-start/common/src/main/java/com/baeldung/common/web/RestResponseEntityExceptionHandler.java‎

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import javax.persistence.EntityNotFoundException;
66
import javax.validation.ConstraintViolationException;
77

8-
import org.slf4j.Logger;
9-
import org.slf4j.LoggerFactory;
108
import org.springframework.dao.DataAccessException;
119
import org.springframework.dao.DataIntegrityViolationException;
1210
import org.springframework.dao.InvalidDataAccessApiUsageException;
@@ -32,29 +30,22 @@
3230

3331
@ControllerAdvice
3432
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
35-
private Logger log = LoggerFactory.getLogger(getClass());
36-
37-
public RestResponseEntityExceptionHandler() {
38-
super();
39-
}
40-
41-
// API
42-
33+
4334
// 400
4435

4536
@Override
4637
protected final ResponseEntity<Object> handleHttpMessageNotReadable(final HttpMessageNotReadableException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
47-
log.info("Bad Request: {}", ex.getMessage());
48-
log.debug("Bad Request: ", ex);
38+
logger.info("Bad Request: ", ex);
39+
logger.debug("Bad Request: ", ex);
4940

5041
final ApiError apiError = message(HttpStatus.BAD_REQUEST, ex);
5142
return handleExceptionInternal(ex, apiError, headers, HttpStatus.BAD_REQUEST, request);
5243
}
5344

5445
@Override
5546
protected final ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
56-
log.info("Bad Request: {}", ex.getMessage());
57-
log.debug("Bad Request: ", ex);
47+
logger.info("Bad Request: ", ex);
48+
logger.debug("Bad Request: ", ex);
5849

5950
final BindingResult result = ex.getBindingResult();
6051
final List<FieldError> fieldErrors = result.getFieldErrors();
@@ -64,9 +55,9 @@ protected final ResponseEntity<Object> handleMethodArgumentNotValid(final Method
6455
}
6556

6657
@ExceptionHandler(value = { ConstraintViolationException.class, DataIntegrityViolationException.class })
67-
public final ResponseEntity<Object> handleBadRequest(final RuntimeException ex, final WebRequest request) {
68-
log.info("Bad Request: {}", ex.getLocalizedMessage());
69-
log.debug("Bad Request: ", ex);
58+
protected final ResponseEntity<Object> handleBadRequest(final RuntimeException ex, final WebRequest request) {
59+
logger.info("Bad Request: ", ex);
60+
logger.debug("Bad Request: ", ex);
7061

7162
final ApiError apiError = message(HttpStatus.BAD_REQUEST, ex);
7263
return handleExceptionInternal(ex, apiError, new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
@@ -76,7 +67,7 @@ public final ResponseEntity<Object> handleBadRequest(final RuntimeException ex,
7667

7768
@ExceptionHandler({ EntityNotFoundException.class, MyEntityNotFoundException.class, MyResourceNotFoundException.class })
7869
protected ResponseEntity<Object> handleNotFound(final RuntimeException ex, final WebRequest request) {
79-
log.warn("Not Found: {}", ex.getMessage());
70+
logger.warn("Not Found: ", ex);
8071

8172
final ApiError apiError = message(HttpStatus.NOT_FOUND, ex);
8273
return handleExceptionInternal(ex, apiError, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
@@ -86,7 +77,7 @@ protected ResponseEntity<Object> handleNotFound(final RuntimeException ex, final
8677

8778
@ExceptionHandler({ InvalidDataAccessApiUsageException.class, DataAccessException.class, MyConflictException.class })
8879
protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final WebRequest request) {
89-
log.warn("Conflict: {}", ex.getMessage());
80+
logger.warn("Conflict: ", ex);
9081

9182
final ApiError apiError = message(HttpStatus.CONFLICT, ex);
9283
return handleExceptionInternal(ex, apiError, new HttpHeaders(), HttpStatus.CONFLICT, request);
@@ -96,7 +87,7 @@ protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final
9687

9788
@ExceptionHandler({ InvalidMimeTypeException.class, InvalidMediaTypeException.class })
9889
protected ResponseEntity<Object> handleInvalidMimeTypeException(final IllegalArgumentException ex, final WebRequest request) {
99-
log.warn("Unsupported Media Type: {}", ex.getMessage());
90+
logger.warn("Unsupported Media Type: ", ex);
10091

10192
final ApiError apiError = message(HttpStatus.UNSUPPORTED_MEDIA_TYPE, ex);
10293
return handleExceptionInternal(ex, apiError, new HttpHeaders(), HttpStatus.UNSUPPORTED_MEDIA_TYPE, request);
@@ -105,7 +96,7 @@ protected ResponseEntity<Object> handleInvalidMimeTypeException(final IllegalArg
10596
// 500
10697

10798
@ExceptionHandler({ NullPointerException.class, IllegalArgumentException.class, IllegalStateException.class })
108-
public ResponseEntity<Object> handle500s(final RuntimeException ex, final WebRequest request) {
99+
protected ResponseEntity<Object> handle500s(final RuntimeException ex, final WebRequest request) {
109100
logger.error("500 Status Code", ex);
110101

111102
final ApiError apiError = message(HttpStatus.INTERNAL_SERVER_ERROR, ex);

0 commit comments

Comments
 (0)