/java/

Supporting XML and JSON Spring 3.1.3

2013-01-24 00:38:40

http://localhost:8080/louis/rest/json/post/1/ http://localhost:8080/louis/rest/json/post/

@Controller
@RequestMapping(value = "/rest/json/post")
public class PostApiJsonController {
    @Autowired
    public PostService postService;

    @RequestMapping(value = "/", method = RequestMethod.GET, produces = "application/json")
    @ResponseStatus(HttpStatus.OK)
    public @ResponseBody PostsCollection getAll(HttpServletResponse response)
    {
        return new PostsCollection(postService.getPosts());
    }


    @RequestMapping(value = "/{postId}/", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public Post getOneInXML(@PathVariable("postId") Long postId)
    {
        return postService.getOne(postId);
    }

}

http://localhost:8080/louis/rest/xml/post/1/ http://localhost:8080/louis/rest/xml/post/

@Controller
@RequestMapping(value = "/rest/xml/post")
public class PostApiXmlController {
    @Autowired
    public PostService postService;

    @RequestMapping(value = "/", method = RequestMethod.GET, produces = "application/xml")
    @ResponseStatus(HttpStatus.OK)
    public @ResponseBody PostsCollection getAll(HttpServletResponse response)
    {
        return new PostsCollection(postService.getPosts());
    }


    @RequestMapping(value = "/{postId}/", method = RequestMethod.GET, produces = "application/xml")
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public Post getOne(@PathVariable("postId") Long postId)
    {
        return postService.getOne(postId);
    }
}

<dependency>
<groupid>org.codehaus.jackson</groupid>
<artifactid>jackson-mapper-asl</artifactid>
<version>1.7.1</version>
</dependency>