POST中的Grails 3应用程序中的标题不会随服务的其余部分一起发送 [英] Headers in POST in Grails 3 app are not being sent with rest of service

查看:126
本文介绍了POST中的Grails 3应用程序中的标题不会随服务的其余部分一起发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Grails 3.0.9,并在 gradle.build 中抓取最新的REST API:

  compile'org.grails:grails-datastore-rest-client:4.0.7.RELEASE',{
''commons-codec','grails-async',' grails-core',
'grails-plugin-converters','grails-web','groovy']。 $ b

我正在尝试进行以下POST请求:

  def rest = new RestBuilder(标题:[X-LSS-Env:devmo],connectTimeout:10000,readTimeout:20000)
response = rest.post (http://..../ ..){
接受application / json
contentTypeapplication / json
json jsonBuilder
}

现在,POST接收器得到json好了,回复一个响应,但这是问题:它接收作为空映射的标题或 null

那么,将标题数据传递给POST接收器的正确方法是什么?这是必要的,因为环境密钥 X-LSS-Env 可以具有不同的值,这指示接收器根据它进行进一步的路由。


$ b

* UPDATE *



消费者我的POST请求实际上是一个运行在 Apache Tomcat / 8.0.26 上的Java应用程序。服务在另一端看起来如何:

  private javax.servlet.http.HttpServletRequest hsr; 
@POST
@Path(/ na)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response postSomething(Ggfp ggfp ){
尝试{
枚举< String> hnames = hsr.getHeaderNames();
int i = 0;
while(hnames.hasMoreElements()){
String headerName = hnames.nextElement();

System.out.println(++ i +headerName:+ headerName);
String val = hsr.getHeader(headerName);
System.out.println(val:+ val);
}
String hval = hsr.getHeader(X-LSS-Env);
return Response.status(Status.OK).entity(X-LSS-Env is+ hval).build();
} catch(Exception e){
}
}

从Postman工作中调用此服务,会标识标头。从Grails应用程序调用它的结果到一个空的映射 - 就像我没有发送头文件一样!

解决方案

RestBuilder 构造函数从来不喜欢我使用(或滥用)它的方式。

  def makePostWsr(serviceUrl)这里是一个干净的方式来实现我设定要做的事情,如果超时发生,使用tryCatch逻辑。 ,jsonBuilder){
try {
def rest = new RestBuilder(connectTimeout:connectTimeout,readTimeout:readTimeout)
def response = rest.post($ wsUrl / $ serviceUrl){
header'X-LSS-Env','devmo'
接受application / json
contentTypeapplication / json
json jsonBuilder
}
response
} catch(Exception e){
println==问题makePostWsr on $ serviceUrl
null
}
}


Using Grails 3.0.9, and grabbing the freshest REST API with this snippet in gradle.build:

compile 'org.grails:grails-datastore-rest-client:4.0.7.RELEASE', {
    ['commons-codec', 'grails-async', 'grails-core',
     'grails-plugin-converters', 'grails-web', 'groovy'].each {
        exclude module: it
    }
}

I am trying to make the following POST request:

def rest = new RestBuilder(headers:["X-LSS-Env":"devmo"], connectTimeout:10000, readTimeout:20000)
response = rest.post("http://..../..") {
    accept "application/json"
    contentType "application/json"
    json jsonBuilder
}

Now, the POST receiver gets the json okay, give back a response okay, but this is the problem: it receives the headers as an empty map or as null!

So, what is the correct way of passing header data to the POST receiver? This is needed because the environment key X-LSS-Env could have different values, which instructs the receiver to do further routing based on it. Same with the GET request of course.

* UPDATE *

The consumer of my POST requests is actually a Java application, running on Apache Tomcat/8.0.26. The is how the service looks on the other side:

private javax.servlet.http.HttpServletRequest hsr;
@POST
@Path("/na")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response postSomething(Ggfp ggfp ){
    try {
        Enumeration<String> hnames = hsr.getHeaderNames();
        int i = 0;
        while (hnames.hasMoreElements()) {
            String headerName = hnames.nextElement();

            System.out.println(++i+ " headerName: " + headerName);
            String val = hsr.getHeader(headerName);
            System.out.println("         val: " + val);
        }
        String hval = hsr.getHeader("X-LSS-Env");
        return Response.status(Status.OK).entity("X-LSS-Env is " + hval).build();
    } catch (Exception e) {
    }
}

Calling this service from Postman works, headers are identified. Calling it from the Grails app results into an empty map - like I am sending no headers!

解决方案

The RestBuilder constructor never liked the way I used (or abused) it. Here is a clean way of achieving what I set out to do, with tryCatch logic if a timeout transpires.

def makePostWsr(serviceUrl, jsonBuilder) {
  try {
     def rest = new RestBuilder(connectTimeout:connectTimeout, readTimeout:readTimeout)
     def response = rest.post("$wsUrl/$serviceUrl") {
        header 'X-LSS-Env', 'devmo'
        accept "application/json"
        contentType "application/json"
        json jsonBuilder
     }
     response
  } catch (Exception e) {
     println "== problem makePostWsr on $serviceUrl"
     null
  }
}

这篇关于POST中的Grails 3应用程序中的标题不会随服务的其余部分一起发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆