001package ball.spring.jig;
002/*-
003 * ##########################################################################
004 * Reusable Spring Components
005 * $Id: BeanRestController.html 6483 2020-07-17 18:12:42Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/hcf-dev/blog/2020-07-19-spring-boot-part-06/src/main/resources/javadoc/src-html/ball/spring/jig/BeanRestController.html $
007 * %%
008 * Copyright (C) 2018 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import java.util.NoSuchElementException;
024import lombok.NoArgsConstructor;
025import lombok.ToString;
026import lombok.extern.log4j.Log4j2;
027import org.springframework.beans.factory.NoSuchBeanDefinitionException;
028import org.springframework.context.ApplicationContext;
029import org.springframework.context.ApplicationContextAware;
030import org.springframework.web.bind.annotation.ExceptionHandler;
031import org.springframework.web.bind.annotation.PathVariable;
032import org.springframework.web.bind.annotation.RequestMapping;
033import org.springframework.web.bind.annotation.ResponseBody;
034import org.springframework.web.bind.annotation.ResponseStatus;
035import org.springframework.web.bind.annotation.RestController;
036
037import static org.springframework.http.HttpStatus.NOT_FOUND;
038import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
039import static org.springframework.http.MediaType.APPLICATION_XML_VALUE;
040import static org.springframework.web.bind.annotation.RequestMethod.GET;
041
042/**
043 * Bean {@link RestController} implementation.
044 *
045 * {@injected.fields}
046 *
047 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
048 * @version $Revision: 6483 $
049 */
050@RestController
051@RequestMapping(value = { "/jig/bean/" })
052@ResponseBody
053@NoArgsConstructor @ToString @Log4j2
054public class BeanRestController implements ApplicationContextAware {
055    private ApplicationContext context = null;
056
057    @Override
058    public void setApplicationContext(ApplicationContext context) {
059        this.context = context;
060    }
061
062    @RequestMapping(method = { GET }, value = { "{name}.json" },
063                    produces = APPLICATION_JSON_VALUE)
064    public Object json(@PathVariable String name) throws Exception {
065        return context.getBean(name);
066    }
067
068    @RequestMapping(method = { GET }, value = { "{name}.xml" },
069                    produces = APPLICATION_XML_VALUE)
070    public Object xml(@PathVariable String name) throws Exception {
071        return context.getBean(name);
072    }
073
074    @ExceptionHandler({
075                           NoSuchBeanDefinitionException.class,
076                               NoSuchElementException.class
077                     })
078    @ResponseStatus(value = NOT_FOUND, reason = "Resource not found")
079    public void handleNOT_FOUND() { }
080}