Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC 3.1 + RESTful web service @GET problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC 3.1 + RESTful web service @GET problem

    I am using spring 3.1 to create RESTful web service, but i a getting a particular problem while doing a get call.

    Let me first publish the service class

    Code:
    package com.techiekernel.service;
    
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import com.techiekernel.model.FooBar;
    
    @Controller
    @RequestMapping("/foobar")
    public class FooBarService {
    
    static Set<FooBar> fooBars;
    
    static {
    fooBars = new HashSet<FooBar>();
    FooBar foobar = null;
    for (int i = 0; i < 10; i++) {
    foobar = new FooBar(i, "Techie Kernel " + i);
    fooBars.add(foobar);
    }
    }
    
    @RequestMapping(value = "/{foobarId}", method = RequestMethod.GET, headers = "Accept=application/xml, application/json", produces = {
    "application/json", "application/xml" })
    @ResponseBody
    public FooBar getFoobar(@PathVariable int foobarId) {
    for (FooBar foobar : fooBars) {
    if (foobar.getId() == foobarId)
    return foobar;
    }
    return null;
    }
    
    @RequestMapping(method = RequestMethod.GET, headers = "Accept=application/xml, application/json", produces = {
    "application/json", "application/xml" })
    @ResponseBody
    public Set<FooBar> getFoobars() {
    return fooBars;
    }
    
    @RequestMapping(value = "/{foobarId}", method = RequestMethod.PUT, headers = "Accept=application/xml, application/json", produces = {
    "application/json", "application/xml" }, consumes = {
    "application/json", "application/xml" })
    @ResponseBody
    public FooBar editFoobar(@RequestBody FooBar foobar,
    @PathVariable int foobarId) {
    for (FooBar foobar1 : fooBars) {
    if (foobarId == foobar1.getId()) {
    foobar1.setId(foobar.getId());
    foobar1.setName(foobar.getName());
    return foobar1;
    }
    }
    return null;
    }
    
    @RequestMapping(value = "/{foobarId}", method = RequestMethod.DELETE, headers = "Accept=application/xml, application/json", produces = {
    "application/json", "application/xml" })
    @ResponseBody
    public boolean deleteFoobar(@PathVariable int foobarId) {
    System.out.println("Delete call.");
    Iterator<FooBar> fooIterator = fooBars.iterator();
    while (fooIterator.hasNext()) {
    FooBar foobar = fooIterator.next();
    System.out.println(foobar);
    if (foobar.getId() == foobarId) {
    fooIterator.remove();
    return true;
    }
    }
    return false;
    }
    
    @RequestMapping(method = RequestMethod.POST, headers = "Accept=application/xml, application/json", produces = {
    "application/json", "application/xml" }, consumes = {
    "application/json", "application/xml" })
    @ResponseBody
    public boolean createFoobar(@RequestBody FooBar fooBar) {
    return fooBars.add(fooBar);
    }
    
    }
    For this when I am trying to get all foobars the "accept:application/json" works fine, but "accept:application/xml" does not work. Bellow is the method, I am interacting for that.

    Code:
    @RequestMapping(method = RequestMethod.GET, headers = "Accept=application/xml, application/json", produces = {
    "application/json", "application/xml" })
    @ResponseBody
    public Set<FooBar> getFoobars() {
    return fooBars;
    }
    When I am making a call to the web service using the curl unix tool, I am getting a "HTTP Status 406".

    curl -i http://springmvc-rest.cloudfoundry.com/foobar -H "accept:application/xml" -X GET

    I have shared the complete example project at https://github.com/karasatishkumar/SpringMVC-REST and also deployed the application in cloud http://springmvc-rest.cloudfoundry.com/.
Working...
X