Grails generate-controller makes a bad restful controller - rest

I'm creating a restful api with grails 3.0.9 but if I generate the controller through the command line with: grails generate-controller [domain]
then my application will give an Grails runtime exception when I make a call to the api.
If I in the other hand create the controller manually (which extends the RestfulController) then there are no problems. I would like to get the controller generated so I can do some checks in save(), delete() and update()
This is the exception that I get:
ERROR org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[grailsDispatcherServlet] - Servlet.service() for servlet [grailsDispatcherServlet] in context with path [] threw exception [Could not resolve view with name 'index' in servlet with name 'grailsDispatcherServlet'] with root cause
javax.servlet.ServletException: Could not resolve view with name 'index' in servlet with name 'grailsDispatcherServlet'
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1227) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]
It looks like it is trying to send the result to a view that does not exist, I should not need to create specific views for my rest api since it should only reply with the http status codes and some JSON.
Is this a bug or is the command generate-controller not designed for this?

I suspect you are generating a request that doesn't specify a format and doesn't specify an Accept header. If one of those is used to indicate that you want a JSON response, the default generated controller will respect that.
The project at https://github.com/jeffbrown/generatedcontroller demonstrates that the default generated controller with Grails 3.0.9 seems to work fine without any views.

What I an recommend is for you to install the templates and customize it so that it would produce a proper Restful controller.

Related

How to redirect to error page, If the URL is invalid?

I am beginner to play scala framework. And I have created simple CRUD operation using it.
In my routes file I have declared all the necesary urls for different actions.
I want to know if user input some wrong url path from browser How should I display error page in play scala? Right now I am getting all the routes which are already defined.
I just found a solution for it
https://www.playframework.com/documentation/2.8.x/ScalaErrorHandling
this is the latest play scala documentation which handles such type of condition.
Since I am using runtime dependency injection (e.g. Guice), the error handler can be dynamically loaded at runtime.
The simplest way is to create a class in the root package called ErrorHandler that implements HttpErrorHandler.
If you place your error handler in the root package (i.e. package-less) and name it ErrorHandler, Play will use it by default.
But, in case you want to:
Add it inside a package;
Configure different error handlers for different environments;
Then add in application.conf the configuration property play.http.errorHandler pointing to your custom error handler class:
play.http.errorHandler = "com.example.ErrorHandler"

The error creating app Engine's DataStore Entity within GWT app

I try to create the entity like this:
DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
Entity stock = new Entity("Stock", 1);
stock.setProperty("Stock", "FCB");
ds.put(stock);
but keep getting the error:
No source code is available for type com.google.appengine.api.datastore.DatastoreService; did you forget to inherit a required module?
The error means just what it says, the GWT compiler needs access to the Java source it compiles to Javascript, and obviously DatastoreService is not something that should exist on the frontend - so you have an architecture issue here.
You'll need to write a proxy that can call a server component (Which in turns calls the DatastoreService) and returns DTOs/value objects (that you define and thus have the source for).
Cheers,
No source code is available
GWT transliterate Java to Javascript, reading it's source code and there a limited language support.
What you're trying to achieve is a Server only operation and you're adding this operation within the client code, which will run on a browser. Neither GAE allow this or GWT has the source of these classes nor capability to do so.
Solution
You need to create a request to your server that will access the DatastoreService , the return the output to the client code.
Below a example of a properly architect GWT web application:

does implementing grails rest controllers needs changes in the UrlMappings?

I am trying to imeplement a rest controller in grails 2.3.7. I have a simple controller, same actions as of a scaffolded one, nothing special.
My problem is I am not able to call show, update, delete and save actions via:
GET to localhost:8080/proj/domain/1
PUT/DELETE to localhost:8080/proj/domain/1
POST to localhost:8080/proj/domain
However it works when I add this to the url in UrlMappings.groovy
"/$controller/$action?/$id?(.$format)?"{
action = [POST:"save",GET:'show',DELETE:"delete"]
}
Im following with the grails doc's '8.1.5 Implementing REST controllers'. Based on my understanding of it, it should work without doing further configurations outside of the controller. Is modifying the url mappings necessary?
Yes adding a REST controller requires you to add a URL mapping for the resource, defining it as either singular or multi resource. Example:
“/foo”(resource:”foo”)
Or
“/foos”(resources:”foo”)
You can run url-mappings-report to see the URL mappings this produces

Action Helper by name EntityManger not found

Just started building an app with the zend framework. I've working on a zend server 5.5. When I try to open the first controller I created, I get the following error message:
Application Error
Message: Action Helper by name EntityManager not found
Stack trace:
#0 c:\Program Files (x86)\Zend\ZendServer\share\ZendFramework\library\Zend\Controller\Action\HelperBroker.php(293): Zend_Controller_Action_HelperBroker::_loadHelper('EntityManager')
...
My guess is that I'm missing a library, but I'm not sure which one. Anyone know?
Where do you register the Helper?
Do you register the helper in the Boostrap or in the application.ini?
Where is the helper located? Please try to give more information concering that.
Anyways, it looks like you are using a standard setup, so you have to register the /path/to/your/helper with the Action Helper Broker.
For indepth information check the documentation
Check the class name of your helper file. The name must be something like this:
Zend_Controller_Action_Helper_helperfilename
This had worked for me.

zend framework controller not found?

I downloaded the latest version of Zend framework, added a controller and I
can not get it to load.. Here is what I did:
C:\zend\Apache2\htdocs>zf create project myproject
Creating project at C:/zend/Apache2/htdocs/myproject
Note: This command created a web project, for more information setting up your V
HOST, please see docs/README
C:\zend\Apache2\htdocs>cd myproject
C:\zend\Apache2\htdocs\myproject>zf create controller mycontroller
Note: The canonical controller name that is used with other providers is "Mycont
roller"; not "mycontroller" as supplied
Creating a controller at C:\zend\Apache2\htdocs\myproject/application/controller
s/MycontrollerController.php
Creating an index action method in controller Mycontroller
Creating a view script for the index action method at C:\zend\Apache2\htdocs\myp
roject/application/views/scripts/mycontroller/index.phtml
Creating a controller test file at C:\zend\Apache2\htdocs\myproject/tests/applic
ation/controllers/MycontrollerControllerTest.php
Updating project profile 'C:\zend\Apache2\htdocs\myproject/.zfproject.xml'
C:\zend\Apache2\htdocs\myproject>
Then I tried to hit the controller from the browser..
http://localhost/myproject/public/mycontroller/
and I get the error: Not Found
The requested URL /myproject/public/mycontroller/ was not found on this server.
I have no idea how to resolve this, and I'm sort of shocked I'm having problems
with the Zend Server.
Dooh.. Actually the problem was quite simple.. I forgot the mod_rewrite.. Strange Zend didn't turn it on by default.
Check out the Zend Quick Start. It looks like you don't have the document root setup properly. Your request should have been http://localhost/myproject/mycontroller or http://myproject/mycontroller - if setup properly.