Grails with JAX-RS vs UrlMappings for RESTful Services

↘锁芯ラ 提交于 2019-12-13 01:54:27

问题


I started out looking at the JAX-RS plugin for grails and thought that was the way to go mainly because it was based on JSR-311 and I figure following standards is usually the smart thing to do. However, using Grail's UrlMappings it seems I basically achieve the same thing. I figure I'm missing something, however, we aren't doing anything overly complex. We basically just need to expose CRUD via an API. Example of doing the same thing with both versions:

JAX-RS:

@PUT
@Consumes(['application/json'])
@Produces(['application/json'])
Response putUser(User user) {
  user.save(flush:true)
  ok user
}

Grails:

def update = {
  def user = new User(params['user'])
  user.save(flush:true)
  render user as JSON
}

Obviously, this is an overly-simplified example and like I said, maybe I'm missing something important. Also, the nice thing about the Grails built in mechanism is I can utilize Content Negotiation along with it.

Anyone have any opinions on this?


回答1:


I had to make the same decision, and I found it just easier to use URL Mappings because the API was not that complex and there were a limited number of API calls that needed to supported.

If came down to what would be easier to maintain based on the LOE and the resources able to support the implementation.




回答2:


The jax-rs plugin is very useful if you are creating web services straight to your domain models. It gives you a "generate-resource" command that automatically creates CRUD apis for your model.

grails generate-resource mydomain.Model

This part seems to work fine, however, I encountered quite a few bugs/problems with the plugin that I finally had to implement the REST services using URL-mappings.

Although the URL-mapping method seems to be more coding, it works perfectly.

import grails.converters.JSON

class ModelServiceController {
    def id = params.id
    def myModel = MyModel.findById(id)
    render myModel as JSON
}

Here's the link for grails REST

http://grails.org/doc/1.0.x/guide/13.%20Web%20Services.html



来源:https://stackoverflow.com/questions/4037253/grails-with-jax-rs-vs-urlmappings-for-restful-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!