001/*
002 * Copyright 2012-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.actuate.autoconfigure.web.servlet;
018
019import java.util.Map;
020
021import org.springframework.boot.web.servlet.error.ErrorAttributes;
022import org.springframework.boot.web.servlet.error.ErrorController;
023import org.springframework.stereotype.Controller;
024import org.springframework.util.Assert;
025import org.springframework.web.bind.annotation.RequestMapping;
026import org.springframework.web.bind.annotation.ResponseBody;
027import org.springframework.web.context.request.ServletWebRequest;
028
029/**
030 * {@link Controller} for handling "/error" path when the management servlet is in a child
031 * context. The regular {@link ErrorController} should be available there but because of
032 * the way the handler mappings are set up it will not be detected.
033 *
034 * @author Dave Syer
035 * @since 2.0.0
036 */
037@Controller
038public class ManagementErrorEndpoint {
039
040        private final ErrorAttributes errorAttributes;
041
042        public ManagementErrorEndpoint(ErrorAttributes errorAttributes) {
043                Assert.notNull(errorAttributes, "ErrorAttributes must not be null");
044                this.errorAttributes = errorAttributes;
045        }
046
047        @RequestMapping("${server.error.path:${error.path:/error}}")
048        @ResponseBody
049        public Map<String, Object> invoke(ServletWebRequest request) {
050                return this.errorAttributes.getErrorAttributes(request, false);
051        }
052
053}