Checkout this project in my playground that shows how to achieve interplay between a SPA application and a REST backend using the RESTEasy JAX-RS implementation and deployed in the JBoss EAP 6.4.0 server.
Also, this particular application (further into the above project) shows how to achieve JAX-RS deployment with the RESTEasy implementation using either the "scanner" or the "application" approach.
In the context of this travail, I posted this SO question and drew heavily (but not without changes) from this blog post.
| Spring Annotation | JAX-RS Annotation |
|---|---|
@RequestMapping(path = "/troopers" |
@Path("/troopers") |
@RequestMapping(method = RequestMethod.POST) |
@POST |
@RequestMapping(method = RequestMethod.GET) |
@GET |
@RequestMapping(method = RequestMethod.DELETE) |
@DELETE |
@ResponseBody |
N/A |
@RequestBody |
N/A |
@PathVariable("id") |
@PathParam("id") |
@RequestParam("xyz") |
@QueryParam('xyz") |
@RequestParam(value="xyz" |
@FormParam(“xyz”) |
@RequestMapping(produces = {"application/json"}) |
@Produces("application/json") |
@RequestMapping(consumes = {"application/json"}) |
@Consumes("application/json") |
I've tested the below with Spring Web MVC 4.3.5:
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.http.ResponseEntity;
import org.springframework.http.MediaType;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
@RequestMapping(path="/example-of-method-1-of-generating-a-JSON-response", method=RequestMethod.GET)
public ResponseEntity fooBar(final HttpServletRequest request
, @RequestParam(value="json", required=true) String json) {
final HttpHeaders httpHeaders= new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
return new ResponseEntity(GsonHelper.toJson(new FooBar(json)), httpHeaders, HttpStatus.OK);
}
@ResponseBody
@RequestMapping(path="/example-of-method-2-of-generating-a-JSON-response", method=RequestMethod.GET)
public String fooBar2(final HttpServletRequest request
, @RequestParam(value="json", required=true) String json) {
return GsonHelper.toJson(new FooBar(json));
}
...
class FooBar {
public String echo;
public FooBar(String echo) {
this.echo = echo;
}
}