PageRenderTime 24ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/spring-boot-samples/spring-boot-sample-actuator/src/main/java/sample/actuator/SampleController.java

https://gitlab.com/perlilja/spring-boot
Java | 78 lines | 49 code | 14 blank | 15 comment | 0 complexity | cdd45b468e107e5244239d61c00d1735 MD5 | raw file
  1. /*
  2. * Copyright 2012-2015 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package sample.actuator;
  17. import java.util.Collections;
  18. import java.util.Date;
  19. import java.util.LinkedHashMap;
  20. import java.util.Map;
  21. import org.hibernate.validator.constraints.NotBlank;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.context.annotation.Description;
  24. import org.springframework.stereotype.Controller;
  25. import org.springframework.validation.annotation.Validated;
  26. import org.springframework.web.bind.annotation.RequestMapping;
  27. import org.springframework.web.bind.annotation.RequestMethod;
  28. import org.springframework.web.bind.annotation.ResponseBody;
  29. @Controller
  30. @Description("A controller for handling requests for hello messages")
  31. public class SampleController {
  32. @Autowired
  33. private HelloWorldService helloWorldService;
  34. @RequestMapping(value = "/", method = RequestMethod.GET)
  35. @ResponseBody
  36. public Map<String, String> hello() {
  37. return Collections.singletonMap("message",
  38. this.helloWorldService.getHelloMessage());
  39. }
  40. @RequestMapping(value = "/", method = RequestMethod.POST)
  41. @ResponseBody
  42. public Map<String, Object> olleh(@Validated Message message) {
  43. Map<String, Object> model = new LinkedHashMap<String, Object>();
  44. model.put("message", message.getValue());
  45. model.put("title", "Hello Home");
  46. model.put("date", new Date());
  47. return model;
  48. }
  49. @RequestMapping("/foo")
  50. @ResponseBody
  51. public String foo() {
  52. throw new IllegalArgumentException("Server error");
  53. }
  54. protected static class Message {
  55. @NotBlank(message = "Message value cannot be empty")
  56. private String value;
  57. public String getValue() {
  58. return this.value;
  59. }
  60. public void setValue(String value) {
  61. this.value = value;
  62. }
  63. }
  64. }