Related article:
Spring 3.1 Web Service RESTful part01 - JSON and XML
Sample Web Application using Spring MVC Framework
Simple Java app using Spring Framework
Environment:
Jdk1.7.07
Maven 3.0.4
Spring Framework version 3.1.1.RELEASE
Spring Source Tools Suits(STS) 3.0
Download Source code here
To open the source code:
File --> Import --> Maven --> Existing Maven Projects
If you are new to Maven, please visit Maven Tutorial.
In previous post, I created the Web Service RESTful with combination JSON and XML. This post is the extension to separate JSON into one method and XML into one method.
Every thing is the same Spring 3.1 Web Service RESTful part01 - JSON and XML except the EmployeeController.java class.
@Controller
public class EmployeeController {
private Logger logger = LoggerFactory.getLogger(getClass());
/**
* Sample URL: http://localhost:8080/app0142/restful/employeeid/1
*/
@RequestMapping(value="/employeeid/{id}", method=RequestMethod.GET, headers="Accept=application/xml")
public @ResponseBody Employee getEmployeeByIdAsXML(@PathVariable Integer id) {
logger.trace("Serving resource for id As XML: " + id);
return new Employee(id,"WhiteXML","Rose","whiterose","whiterose1@gmail.com","123456789","img_whiterose");
}
@RequestMapping(value="/employeeid/{id}", method=RequestMethod.GET, headers="Accept=application/json")
public @ResponseBody Employee getEmployeeByIdAsJSON(@PathVariable Integer id) {
logger.trace("Serving resource for id As JSON : " + id);
return new Employee(id,"WhiteJSON","Rose","whiterose","whiterose1@gmail.com","123456789","img_whiterose");
}
}
Testing
1)JSON
2)XML