Grails错误Null Pointer Exception访问GSP

时间:2016-06-14 12:55:51

标签: rest grails groovy 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>

注意:我没有域类文件;如果是的话,我是否需要一个 - 请告诉我如何构建它。

感谢。

注意:添加项目目录结构的图片:

grails project directory structure

1 个答案:

答案 0 :(得分:1)

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

如果您可以使用来自&#39; grails-app&#39;的目录发布您的文件名。我们可能会看到问题(例如&#39; /grails-app/views/login/api_test.gsp')