【问题标题】:how to consume an external rest with grails JAX-RS?如何使用 grails JAX-RS 消耗外部休息?
【发布时间】:2011-10-09 00:42:42
【问题描述】:

我一直在寻找整个互联网,试图找到一个如何做到这一点的例子。我只想使用外部 REST 服务器,但我不知道如何设置外部服务器的 url,请帮助

import static org.grails.jaxrs.response.Responses.*

import javax.ws.rs.Consumes
import javax.ws.rs.GET
import javax.ws.rs.Produces
import javax.ws.rs.Path
import javax.ws.rs.PathParam
import javax.ws.rs.POST
import javax.ws.rs.core.Response

**@Path('http://localhost:8080/prueba3/api/person')**
@Consumes(['application/xml','application/json'])
@Produces(['application/xml','application/json'])
class PersonCollectionResource {

    @POST
    Response create(Person dto) {
        created dto.save()
    }

    @GET
    Response readAll() {
        ok Person.findAll()
    }

    @Path('/{id}')
    PersonResource getResource(@PathParam('id') String id) {
        new PersonResource(id:id)
    }

}

【问题讨论】:

    标签: rest grails jax-rs


    【解决方案1】:

    如果你的项目名称是 prubea3,你应该像这样定义你的路径

    @Path('/api/person')
    

    您的休息服务器可以运行另一台机器。这对你来说并不重要。如果您的休息服务器在 localhost:8080 上运行,您应该发出这样的请求

    http://localhost:8080/prueba3/api/person
    

    希望对你有用。

    【讨论】:

    • '/api' 部分是强制性的吗?我们可以把 api 换成别的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 2012-07-27
    • 1970-01-01
    相关资源
    最近更新 更多