Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @JSONRequestMapping(value = "/foo", method = RequestMethod.POST)
- @Target({ElementType.METHOD, ElementType.TYPE})
- @Retention(RetentionPolicy.RUNTIME)
- @RequestMapping(consumes = "application/json", produces = "application/json")
- public @interface JsonRequestMapping {
- @AliasFor(annotation = RequestMapping.class, attribute = "value")
- String[] value() default {};
- @AliasFor(annotation = RequestMapping.class, attribute = "method")
- RequestMethod[] method() default {};
- @AliasFor(annotation = RequestMapping.class, attribute = "params")
- String[] params() default {};
- @AliasFor(annotation = RequestMapping.class, attribute = "headers")
- String[] headers() default {};
- @AliasFor(annotation = RequestMapping.class, attribute = "consumes")
- String[] consumes() default {};
- @AliasFor(annotation = RequestMapping.class, attribute = "produces")
- String[] produces() default {};
- }
- @JsonRequestMapping(method = POST)
- public String defaultSettings() {
- return "Default settings";
- }
- @JsonRequestMapping(value = "/override", method = PUT, produces = "text/plain")
- public String overrideSome(@RequestBody String json) {
- return json;
- }
- package com.example;
- import org.springframework.http.MediaType;
- import org.springframework.web.bind.annotation.*;
- @RestController
- @RequestMapping(path = "/",
- consumes = MediaType.APPLICATION_JSON_VALUE,
- produces = MediaType.APPLICATION_JSON_VALUE,
- method = {RequestMethod.GET, RequestMethod.POST})
- public class JsonProducingEndpoint {
- private FooService fooService;
- @RequestMapping(path = "/foo", method = RequestMethod.POST)
- public String postAFoo(@RequestBody ThisIsAFoo theFoo) {
- fooService.saveTheFoo(theFoo);
- return "http://myservice.com/foo/1";
- }
- @RequestMapping(path = "/foo/{id}", method = RequestMethod.GET)
- public ThisIsAFoo getAFoo(@PathVariable String id) {
- ThisIsAFoo foo = fooService.getAFoo(id);
- return foo;
- }
- @RequestMapping(path = "/foo/{id}", produces = MediaType.APPLICATION_XML_VALUE, method = RequestMethod.GET)
- public ThisIsAFooXML getAFooXml(@PathVariable String id) {
- ThisIsAFooXML foo = fooService.getAFoo(id);
- return foo;
- }
- }
Add Comment
Please, Sign In to add comment