Search code examples
restgrailsgrails-2.0grails-controller

Grails 2.x REST Controller - works in empty project, not the 'real' project


I am fairly sure I have something simple setup incorrectly in my project, but I sure could use some help finding out what!

I have the latest GGTS and Grails 2.4.3. I tried to make a REST Controller in a project that two of us are working on. As this was new to me, when it didn't work, I found an article that showed how simple it was and followed it exactly in a new, empty project. Everything worked properly and as advertised! I used grails commands to create the project, the domain class and then the controller.

I went back to my original project and did the same steps (except for creating the project which already existed, but WAS created via the grails command). Instead of working, when I hit the rest URL via a GET it returns a HTTP 404 status and a message saying that it can not find 'index.gsp'. Of course, index.gsp does not exist. However, it should return json as requested. My question is why?

Since I am (now) just trying to get the REST calls working, the Domain class is trivial - three String fields. No other relationships. The Controller extends RestfulController. No static scaffold or anything like that....

What is the same in each project?

Most of the setup is original and untouched. Both the URLMapping files are identical. Both of the Config.groovy files are identical.

What is different between the two projects?

The main difference is that three (3) non-REST controllers already exist. This is far and away the biggest difference that I see. Is there an issue / need to configure something when 'mixing' Controllers (REST and nonRest) or should they peacefully co-exist?

The only other difference is the existing project (that does not work) has AngularJS and Bootstrap. Nothing else has been added.

Any ideas appreciated!

Gory details

The curl command used to hit the endpoint:

curl  -H "Accept: application/json" http://localhost:8080/myproject/example

response:

<html><head><title>Apache Tomcat/7.0.55 - Error report</title><style><!--H1
[deleted font family lines...]
{color : black;}A.name {color : black;}HR {color : #525D76;}--></style>
</head><body><h1>HTTP Status 404 - &quot;/WEB-INF/grails-app/views/example/index.gsp&quot; not found.</h1>
<HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b>
<u>&quot;/WEB-INF/grails-app/views/example/index.gsp&quot; not found.</u></p><p> 
<b>description</b> <u>The requested resource is not available.</u></p>
<HR size="1" noshade="noshade"><h3>ApacheTomcat/7.0.55</h3></body></html>

Domain Class:

package com.mycompany.rest

import groovy.transform.EqualsAndHashCode;
import groovy.transform.ToString;

@ToString(includeNames = true, includeFields = true, excludes = 'dateCreated, lastUpdated')
@EqualsAndHashCode
class Example {

    Long id
    Long version
    Date dateCreated
    Date lastUpdated

    String name
    String description


    static constraints = {
        name blank:false, nullable:false
        description blank:false, nullable: false
    }
}

Controller:

package com.mycompany.rest

import grails.rest.RestfulController;

class ExampleController extends RestfulController<Example> {

    static responseFormats = ['json', 'xml']

    ExampleController() {
            super(Example)
    }


}

Solution

  • While I never figured out exactly what was causing the behavior I described, it was obviously something strange I did in the original project somewhere!

    I resolved the issue by creating a third brand new, empty project. I added my rest service as described above and verified that worked - naturally it did. Then, because the work thus far was still not very large, I copied each controller/domain object/view/etc into the new project. Everything now works. Kind of a wimpy way to resolve the issue I know, but as I suspect I somehow put the first project into a highly unlikely state anyway, I am just happy to be back functional!

    Also - thanks Burt for the other comments - have incorporated them all.