[coldbox:28024] Rest Pagination

If you’re using one of the REST templates, then the Response object which generates that pagination struct is located at coldbox/system/web/context/Response.cfc

If you want to change the pagination structure, then you will need to clone that component ( or extend it ) and modify the setPagination method to your desired structure.

Then, add a preProcess interception ( which will fire before the aroundHandler method in the base RestHandler.cfc ) which sets prc.response to your new component.

From that point on, it should work exactly as it currently does, with a different pagination structure.

HTH,

Jon

image001.png

Excuse me Jon,

but I can’t set my custom response.

I added “preHandler” in my Echo.cfc, like this:

prc-Immagine 2020-12-07 100931.png

objs.jpg

“prc” is a struct. You would need to use dot notation to set that value.

prc-Immagine 2020-12-07 100931.png

objs.jpg

I realize that this thread is frrom a while ago, but I’d appreciate if you could post some code snippets to show what the two files look like. The Response.cfc and the interceptor.

I’m also wanting to present my own JSON response, rather than use the one that comes by default in the rest-hmvc template – which includes pagination as part of every response.

Thanks

I’ve answered my own question… got it to work…

Here is the code in case anyone else comes across this thread.

First create an interceptor cfc. Mine is saved as interceptors/SimpleResponse.cfc

component{

/* :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */

function preProcess( event, interceptData, rc, prc ){
	prc.response = wirebox.getInstance( "SimpleResponse" );
  }

/* :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */

}

Second, create models/SimpleResponse.cfc

component extends="coldbox.system.web.context.Response" hint=""{

/* :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */

function init(){
	super.init()
}

/* :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */

	/**
 * Returns a standard response formatted data packet using the information in the response
 *
 * @reset Reset the 'data' element of the original data packet
 */
struct function getDataPacket( boolean reset = false ){
	var packet = {
		"error"      : getError() ? true : false,
		"messages"   : getMessages(),
		"data"       : getData()
	};

	// Are we reseting the data packet
	if ( arguments.reset ) {
		packet.data = {};
	}

	return packet;
}

/* :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */

}

Note that I’ve removed the ‘pagination’ key completely. Compare the getDataPacket() method to the one in coldbox.system.web.context.Response and you’ll see the difference.

Third, add the interceptor to config/Coldbox.cfc

		interceptors = [
        {
			class      : "interceptors.SimpleResponse",
				name       : "SimpleResponse",
				properties : {}
			}
	];

Fourth, reinit your Coldbox app.

And Lastly, yell out, BLOODY AMAZING!

Working with CB is a real treat!

My response now looks like this:

{
  "data": {
    "hash": "$2a$12$.8E7U9SRc2ZYgKv9KtzsI.Z9hVI8IJK3d0DcLoU7pCe5sSbSqxrUe"
  },
  "error": false,
  "messages": []
}
2 Likes