Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX with Spring MVC

What AJAX libraries work well with Spring MVC?

I'm new to developing with Spring and Spring MVC. From the documentation at http://www.springsource.org I'm not yet understanding what AJAX framework Spring MVC has built-in or what third party APIs and tooling might be suggested as working well with developing a Spring MVC application.

All recommendations are appreciated.

I did search through previous SO discussions on this subject, but I didn't get any clear direction.

like image 893
Robin Doer Avatar asked May 31 '12 03:05

Robin Doer


1 Answers

Spring is super easy to use with Ajax. If Jackson is on the classpath Spring can use it for returning JSON to the caller. Something like this:

@RequestMapping( "/my/path" )
public @ResponseBody MyObject doSomething( @RequestParam Long myVal ) {
    MyObject result = new MyObject( myVal );
    // do something interesting
    return result;
}

Then you can use jQuery (or your other favorite javascript library) to make a request to http://myserver/my/path and handle the resulting JSON object.

Google's GSON is also easy to use. As in:

@RequestMapping( "/my/path" )
public ResponseEntity<String> MyObject doSomething( @RequestParam Long myVal ) {
    MyObject result = new MyObject( myVal );
    // do something interesting
    HttpHeaders headers = new HttpHeaders();
    headers.set(  "Content-Type", "application/json" );
    String json = gson.toJson( result );
    return new ResponseEntity<String>( json, headers, HttpStatus.CREATED );
}
like image 72
digitaljoel Avatar answered Oct 20 '22 08:10

digitaljoel