Search code examples
angularjsspringspring-bootrestful-architecture

How to support both json and html response with Spring Boot


I want to send data to both mobile (using json) and web (using html) and I am using Spring boot. I want to attain similar architecture and functionality as used by facebook(graph.facebook.com and facebook.com) and twitter(api.twitter.com and twitter.com) . Also I am using Angular.js as a web framework.


Solution

  • Nothing prevents you to make two different implementations (json, html) and serve it from same URL. Just use proper Consumable Media Types or Producible Media Types eg:

    @Controller
    @RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, produces="application/json")
    @ResponseBody
    public Pet getPet(@PathVariable String petId, Model model) {
        // implementation omitted
    }