Skip to content

Instantly share code, notes, and snippets.

Mattias Severson matsev

Block or report user

Report or block matsev

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
matsev /
Last active Dec 11, 2019
Generic response error handling using @ControllerAdvice
public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
List<ObjectError> globalErrors = ex.getBindingResult().getGlobalErrors();
List<String> errors = new ArrayList<>(fieldErrors.size() + globalErrors.size());
String error;
for (FieldError fieldError : fieldErrors) {
matsev /
Created Sep 8, 2012
Spring Controller Tests 2.0
public void shouldGetTestUserAsJson() throws Exception {
.andExpect(jsonPath("name", is("Test User")))
.andExpect(jsonPath("email", is("")));
matsev /
Last active Sep 28, 2018
Enhanced error feedback from a Spring Controller
public class ErrorMessage {
private List<String> errors;
public ErrorMessage() {
public ErrorMessage(List<String> errors) {
this.errors = errors;
@RequestMapping(method = RequestMethod.POST,
void create(@RequestBody @Valid User user, HttpServletRequest request, HttpServletResponse response) {
long userId = userService.create(user);
String location = ServletUriComponentsBuilder.fromRequestUri(request).path("/{userid}")
response.addHeader("Location", location);
matsev /
Created Feb 18, 2012
Mockito and Dependency Injection
public class Example {
private Delegate delegate;
Example(Delegate delegate) {
this.delegate = delegate;
public void doIt() {
matsev /
Created Dec 3, 2011
Using Mock Obects in Spring Integration Tests
package com.jayway.example;
public interface Delegate {
String doSomething();
String doSomethingElse();
matsev /
Created Nov 27, 2011
Creating Spring FactoryBeans for Mock Objects
public class BeanWiringTest {
SomeClass someClass;
SomeDependency someDependencyMock;
You can’t perform that action at this time.