krish
krish

Reputation: 90

how to get data from spring controller without annotation

i am using Spring 3.x for my MVC application without annotation. I want to get data only not view . I google it and found it is possible using @ResponseBody . but i dont want to use annotation. how can i tell spring it is only data not a view without annotation. my sample code given below .

public class ShowGraphController extends AbstractController {

    private JdbcUserDao userDao;

    public void setUserDao(JdbcUserDao userDao) {
        this.userDao = userDao;
    }

    protected ModelAndView handleRequestInternal(HttpServletRequest request,HttpServletResponse responce) throws Exception {{
        return new  ModelAndView("want it retun as a data not a view name only");
    }
}

Upvotes: 3

Views: 593

Answers (3)

Adam Gent
Adam Gent

Reputation: 49085

I assume you mean JSON when you say data?

If you have to use ModelAndView style just handle the HttpServletResponse yourself and return null.

Upvotes: 1

Septem
Septem

Reputation: 3622

the easiest way is to use @ResponseBody, but if you do not want to use Annotation, you can populate response yourself:

protected void handleRequestInternal(HttpServletRequest request,HttpServletResponse response) throws Exception {
    response.getWriter().println("want it retun as a data not a view name only");
}

Upvotes: 1

AllTooSir
AllTooSir

Reputation: 49372

It is bit convoluted as with Spring 3, you should ideally be using ResponseBody annotation. Have a look at this class ResponseEntity , this may be useful for your purpose. Sample code from Spring doc :

@RequestMapping("/handle")
public ResponseEntity<String> handle() {
  HttpHeaders responseHeaders = new HttpHeaders();
  responseHeaders.set("MyResponseHeader", "MyValue");
  return new 
     ResponseEntity<String>("Hello World", responseHeaders, HttpStatus.CREATED);
}

Upvotes: 1

Related Questions