Skip to content

anujwalia/problem-spring-web

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Problems for Spring Web MVC

Build Status Coverage Status Release Maven Central

This library offers @ControllerAdvice traits, which makes use of the Problem library to map common exceptions of your Spring Web MVC application to an application/problem+json.

Dependency

<dependency>
    <groupId>org.zalando</groupId>
    <artifactId>problem-spring-web</artifactId>
    <version>${problem-spring-web.version}</version>
</dependency>

Usage

Before you start using this library, make sure you read and understood Exception Handling in Spring MVC.

What Problem Spring Web offers is a bunch of Advice Traits that you can use to create your own Controller Advice. Advice traits are small, reusable @ExceptionHandler methods that are implemented as default methods and placed in single-method interfaces:

public interface NotAcceptableAdviceTrait extends AdviceTrait {

    @ExceptionHandler
    default ResponseEntity<Problem> handleMediaTypeNotAcceptable(
            final HttpMediaTypeNotAcceptableException exception,
            final NativeWebRequest request) {
        return Responses.create(Status.NOT_ACCEPTABLE, exception, request);
    }

}

This allows them to be picked and combined à la carte:

@ControllerAdvice
class ExceptionHandling implements MethodNotAllowedAdviceTrait, NotAcceptableAdviceTrait {

}

There are ~15 different advice traits provided by Problem Spring Web. They are grouped into aggregate advice traits, e.g. the HttpAdviceTrait includes NotAcceptableAdviceTrait, UnsupportedMediaTypeAdviceTrait and MethodNotAllowedAdviceTrait.

public interface HttpAdviceTrait extends
        NotAcceptableAdviceTrait,
        UnsupportedMediaTypeAdviceTrait,
        MethodNotAllowedAdviceTrait {

}

Future versions of this library may add additional traits to those aggregates and in case you want to have all of them, just use ProblemHandling:

@ControllerAdvice
class ExceptionHandling implements ProblemHandling {

}

License

Copyright [2015] Zalando SE

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.

About

Handling Problems in Spring Web MVC

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 99.8%
  • Shell 0.2%