Skip to content
This repository has been archived by the owner on Jul 2, 2023. It is now read-only.

Base resources cause errors in Grails 3.2.4 #31

Open
donald-jackson opened this issue Jan 31, 2017 · 0 comments
Open

Base resources cause errors in Grails 3.2.4 #31

donald-jackson opened this issue Jan 31, 2017 · 0 comments

Comments

@donald-jackson
Copy link

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":"[email protected]","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}
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant