com.fivefaces.structureclient.config.security.JsonErrorResponseSender Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-structure-client Show documentation
Show all versions of common-structure-client Show documentation
structure Client for Five Faces
package com.fivefaces.structureclient.config.security;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
@Component
@RequiredArgsConstructor
public class JsonErrorResponseSender {
private final ObjectMapper objectMapper;
public void sendErrorResponse(HttpServletResponse response, int httpStatus, String message) throws IOException {
response.setStatus(httpStatus);
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
objectMapper.writeValue(response.getWriter(), new HashMap<>(){{
put("message", message);
}});
response.getWriter().flush();
}
}