Guest User

Untitled

a guest
Jan 10th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. @JSONRequestMapping(value = "/foo", method = RequestMethod.POST)
  2.  
  3. @Target({ElementType.METHOD, ElementType.TYPE})
  4. @Retention(RetentionPolicy.RUNTIME)
  5. @RequestMapping(consumes = "application/json", produces = "application/json")
  6. public @interface JsonRequestMapping {
  7. @AliasFor(annotation = RequestMapping.class, attribute = "value")
  8. String[] value() default {};
  9.  
  10. @AliasFor(annotation = RequestMapping.class, attribute = "method")
  11. RequestMethod[] method() default {};
  12.  
  13. @AliasFor(annotation = RequestMapping.class, attribute = "params")
  14. String[] params() default {};
  15.  
  16. @AliasFor(annotation = RequestMapping.class, attribute = "headers")
  17. String[] headers() default {};
  18.  
  19. @AliasFor(annotation = RequestMapping.class, attribute = "consumes")
  20. String[] consumes() default {};
  21.  
  22. @AliasFor(annotation = RequestMapping.class, attribute = "produces")
  23. String[] produces() default {};
  24. }
  25.  
  26. @JsonRequestMapping(method = POST)
  27. public String defaultSettings() {
  28. return "Default settings";
  29. }
  30.  
  31. @JsonRequestMapping(value = "/override", method = PUT, produces = "text/plain")
  32. public String overrideSome(@RequestBody String json) {
  33. return json;
  34. }
  35.  
  36. package com.example;
  37.  
  38. import org.springframework.http.MediaType;
  39. import org.springframework.web.bind.annotation.*;
  40.  
  41. @RestController
  42. @RequestMapping(path = "/",
  43. consumes = MediaType.APPLICATION_JSON_VALUE,
  44. produces = MediaType.APPLICATION_JSON_VALUE,
  45. method = {RequestMethod.GET, RequestMethod.POST})
  46. public class JsonProducingEndpoint {
  47.  
  48. private FooService fooService;
  49.  
  50. @RequestMapping(path = "/foo", method = RequestMethod.POST)
  51. public String postAFoo(@RequestBody ThisIsAFoo theFoo) {
  52. fooService.saveTheFoo(theFoo);
  53. return "http://myservice.com/foo/1";
  54. }
  55.  
  56. @RequestMapping(path = "/foo/{id}", method = RequestMethod.GET)
  57. public ThisIsAFoo getAFoo(@PathVariable String id) {
  58. ThisIsAFoo foo = fooService.getAFoo(id);
  59. return foo;
  60. }
  61.  
  62. @RequestMapping(path = "/foo/{id}", produces = MediaType.APPLICATION_XML_VALUE, method = RequestMethod.GET)
  63. public ThisIsAFooXML getAFooXml(@PathVariable String id) {
  64. ThisIsAFooXML foo = fooService.getAFoo(id);
  65. return foo;
  66. }
  67. }
Add Comment
Please, Sign In to add comment