Grails 错误 Null Pointer Exception 访问 GSP

Grails error Null Pointer Exception accesses GSP

嘿伙计们:我真的被困在这个问题上了。

我有一个使用 REST 服务的 grails 应用程序。 它可以很好地访问 REST 服务,但问题是当我想在 GSP 中呈现结果时。我收到以下错误:

errors.GrailsExceptionResolver  - NullPointerException occurred when processing request: [GET] /TestAPI/login/index
Stacktrace follows:
java.lang.NullPointerException
    at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:198)
    at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

这是我的问题: 1. 我试图将输出发送到不同的 GSP 而不是索引。 2. 我阅读了堆栈跟踪,在我的项目中,我在它说找不到的路径中有 index.gsp。

这是我的控制器代码:

package testapi

import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.client.support.HttpAccessor
import org.springframework.web.client.RestTemplate
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;

import groovyx.net.http.*

import java.util.Base64;
import java.util.Map;


//import org.springframework.web.client.RestTemplate;

class LoginController {

    def index() 
    { 
        String usernm = "xxxxxxxxxx";
        String link = "https://xxx.xxx.xxx/oauth2/token";
        String passwd = "yyyyyy";
        //Base64.encodeBase64(usernm.concat(":".concat(passwd)).getBytes())
        Base64.getEncoder().encode(usernm.concat(":".concat(passwd)).getBytes());
        String base64UserCreds = new String(Base64.getEncoder().encode(usernm.concat(":".concat(passwd)).getBytes()));

        RestTemplate restTemplate = new RestTemplate();

        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("Authorization","Basic ".concat(base64UserCreds));

        HttpEntity<String> request = new HttpEntity<>(requestHeaders);

        System.out.println ("URI under Test is: " + String.valueOf(link) + ". \n" + "Please stand by for results ... \n \n ");
        System.out.println ("Base 64 encoded string is: " + base64UserCreds);

        @SuppressWarnings("rawtypes")
        Map result = restTemplate.exchange(link, HttpMethod.POST, request, Map.class).getBody();

        String authtype = (String) result.get("tokenType");
        String tokenaccess = (String) result.get("accessToken");
        //PrintStream testOutput = null;
        //testOutput.append(result2);
        System.out.println("Access token: " + tokenaccess + "\n");

        HttpHeaders requestHeaders2 = new HttpHeaders();
        requestHeaders2.add("zzzz-pppp-oauth-scope", "zzz:read-any");
        requestHeaders2.add("Authorization","Bearer ".concat(tokenaccess));
        HttpEntity<String> request2 = new HttpEntity<>(requestHeaders2);
        System.out.println("URI #1 under test: https://xxxx.xxxx.xxxx?expand=reps \n");
        String result3 = restTemplate.exchange("https://xxxx.xxxx.xxxx?expand=reps", HttpMethod.GET, request2, String.class).getBody();
        [result3]
        render(view: "api_test", model:[name: result3])
        System.out.println(result3);

    }
}

这是 gsp:

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="layout" content="main"/>
<title>AutoTest - API</title>
</head>
<body>
  <div class="body">

  API Results ${result3}

  </div>
</body>
</html>

注意:我没有域 class 文件;如果需要,我需要一个吗 - 请告诉我如何构建它。

谢谢。

注意:附上项目目录结构图片:

render result3 不会渲染视图,而是渲染 result3 的内容,所以 json 响应并不奇怪。

如果您可以 post 您的文件名与来自 'grails-app' 的目录,我们可能会发现问题(例如'/grails-app/views/login/api_test.gsp')