budjb / grails-jaxrs

JAX-RS Plugin for Grails

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Base resources cause errors in Grails 3.2.4

donald-jackson opened this issue · comments

When using generate resources on a domain class, the base resources no longer function.

Using generated resources and spring security rest. My original pull request fixed the crashing issue to at least get it to start but the side effect was these base resources don't work.

// Collection
package users

import grails.plugin.springsecurity.annotation.Secured

import static org.grails.plugins.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('/api/user')
@Consumes(['application/xml','application/json'])
@Produces(['application/xml','application/json'])
class   UserCollectionResource {

    def userResourceService

    @POST
    Response create(User dto) {
        created userResourceService.create(dto)
    }

    @GET
    @Secured([UserService.ROLE_ADMIN])
    Response readAll() {
        ok userResourceService.readAll()
    }

    @Secured([UserService.ROLE_ADMIN])
    @Path('/{id}')
    UserResource getResource(@PathParam('id') Long id) {
        new UserResource(userResourceService: userResourceService, id:id)
    }
}

// Base
package users

import grails.plugin.springsecurity.annotation.Secured

import javax.ws.rs.Path

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

import javax.ws.rs.Consumes
import javax.ws.rs.DELETE
import javax.ws.rs.GET
import javax.ws.rs.Produces
import javax.ws.rs.PUT
import javax.ws.rs.core.Response

@Consumes(['application/xml','application/json'])
@Produces(['application/xml','application/json'])
class UserResource {

    def userResourceService
    def id

    @GET
    @Secured([UserService.ROLE_ADMIN])
    Response read() {
        ok userResourceService.read(id)
    }

    @PUT
    Response update(User dto) {
        dto.id = id
        ok userResourceService.update(dto)
    }

    @DELETE
    void delete() {
        userResourceService.delete(id)
    }
}

Request list example:

curl -i -H "Accept: application/json" -X GET -H "Authorization: Bearer XXX" "http://localhost:8080/api/user"
HTTP/1.1 200 
X-Application-Context: application:development
Content-Type: application/json
Transfer-Encoding: chunked
Date: Tue, 31 Jan 2017 12:01:58 GMT

[{"id":2,"accountExpired":false,"accountLocked":false,"emailAddress":"admin@admin.com","enabled":true,"password":"$2a$10$gGCLHcmOL0xOHRe/DU88ueMvUEa/SAOi49VG.N0i2SBRJV75gZ6UC","passwordExpired":false}]

Request user ID 2 as provided:

curl -i -H "Accept: application/json" -X GET -H "Authorization: Bearer XXX" "http://localhost:8080/api/user/2"
HTTP/1.1 404 
X-Application-Context: application:development
Content-Type: application/json;charset=UTF-8
Content-Language: en-US
Transfer-Encoding: chunked
Date: Tue, 31 Jan 2017 12:02:01 GMT

{"message":"Not Found","error":404}