小编tim*_*tes的帖子

如何告诉Grails将JSONNull渲染为null?

我有一个从Groovy的HTTPBuilder返回的JSON对象.JSON包含一些表示为JSONNull对象的空值.问题是当我尝试在响应中渲染JSON时,我在尝试渲染JSONNull时出现错误.我得到的回复只是部分呈现的.我希望它呈现为"null".我该怎么做呢?

码:

render(contentType: "text/json") {
    listOfJSONObjectsThatIncludeJSONNulls
}
Run Code Online (Sandbox Code Playgroud)

错误:

| Error 2013-09-17 11:33:56,965 [http-bio-8080-exec-4] ERROR errors.GrailsExceptionResolver  - JSONException occurred when processing request: [GET] /my/action
Object is null. Stacktrace follows:
Message: Object is null
   Line | Method
->>  69 | isEmpty        in net.sf.json.JSONNull
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   199 | …
Run Code Online (Sandbox Code Playgroud)

null grails json grails-2.0.4

2
推荐指数
1
解决办法
3061
查看次数

在Groovy中对Map的值进行排序

我有一个地图,其中一个键包含多个值

datamap = [ 'Antenna Software':[ 'Salarpuria', 'Cessna', 'Vrindavan Tech', 'Alpha Center' ],
             'Ellucian':[ 'Malvern', 'Ellucian House', 'Residency Road'] ] 
Run Code Online (Sandbox Code Playgroud)

在这里,我需要按字母顺序对值进行排序

datamap = [ 'Antenna Software':[ 'Alpha Center', 'Cessna', 'Salarpuria', 'Vrindavan Tech' ],
            'Ellucian':[ 'Ellucian House', 'Malvern', 'Residency Road' ] ] 
Run Code Online (Sandbox Code Playgroud)

如何以时髦的方式做到这一点?

groovy

2
推荐指数
1
解决办法
1664
查看次数

Spock MissingMethodException

我的东西看起来像是类似的规格:

def "my spec"(Record record) {
    given: 
        Something something = getSomething()
    and: 
        otherThing = getOtherThing()

    doFlow(something, record)
    if (record.someType = Types.SOME_SPECIFIC_TYPE) {
        doFlow(something, record)
    } 
}

def doFlow(Something something, Record record) {
    when:
         //code
    then:
         //asserts

    when:
         //code
    and: 
         //mode code
    then:
         //code
}
Run Code Online (Sandbox Code Playgroud)

但是,在运行时,我得到:groovy.lang.MissingMethodException: No signature of method doFlow() is applicable for arguments Something, Record values: [given values].

testing groovy spock

2
推荐指数
1
解决办法
1178
查看次数

我需要从grails应用程序中的Url映射类发送一个参数(它是一个不同的用例)

我有这个

 "/"{controller = "normal"
          action = "index"
      }
Run Code Online (Sandbox Code Playgroud)

这将在我给出时发挥作用localhost:8080,我需要的是我必须通过这个发送一个参数,以便请求开始/将进入指定的控制器和该参数的动作,所以相应地我可以做一些条件来放置一些功能我的看法.所以来自该映射的请求必须发送一些参数,该参数不应该改变我localhost:8080使用该参数的URL .

grails

2
推荐指数
1
解决办法
4352
查看次数

Groovy中是否自动定义了访问器/更改器?

在使用Groovy In ActionGroovy处理Java Bean的部分中,我找到了此脚本(稍作修改):

class Book{
  String title
}
def groovyBook = new Book()
// explicit way
groovyBook.setTitle('What the heck, really ?')
println groovyBook.getTitle()
// short-hand way
groovyBook.title = 'I am so confused'
println groovyBook.title  
Run Code Online (Sandbox Code Playgroud)

该类中没有此类方法,Book那么它如何工作?

groovy

2
推荐指数
1
解决办法
1178
查看次数

groovy split exception java.lang.ArrayIndexOutOfBoundsException:1

我有这个代码来分割线来获取键值对,如果值丢失则抛出执行.

为什么要施行?拆分此键值对线的正确方法是什么.

def lline="name="
def (key, value) = lline.split("=")
Run Code Online (Sandbox Code Playgroud)

错误:

Caught: java.lang.ArrayIndexOutOfBoundsException: 1
java.lang.ArrayIndexOutOfBoundsException: 1
Run Code Online (Sandbox Code Playgroud)

谢谢

groovy

2
推荐指数
1
解决办法
1724
查看次数

用于将json转换为CSV文件的Groovy代码

有没有人有任何样本Groovy代码将JSON文档转换为CSV文件?我试图在Google上搜索但无济于事.

示例输入(来自评论):

[ company_id: '1',
  web_address: 'vodafone.com/',
  phone: '+44 11111',
  fax: '',
  email: '',
  addresses: [ 
      [ type: "office", 
        street_address: "Vodafone House, The Connection",
        zip_code: "RG14 2FN",
        geo: [ lat: 51.4145, lng: 1.318385 ] ]
  ],
  number_of_employees: 91272,
  naics: [
      primary: [ 
          "517210": "Wireless Telecommunications Carriers (except Satellite)" ],
      secondary: [ 
          "517110": "Wired Telecommunications Carriers",
          "517919": "Internet Service Providers",
          "518210": "Web Hosting"
      ]
  ]
Run Code Online (Sandbox Code Playgroud)

更多来自编辑的信息:

def export(){
   def exportCsv = [ [ id:'1', color:'red', planet:'mars', description:'Mars, the "red" planet'], 
                     [ …
Run Code Online (Sandbox Code Playgroud)

groovy

2
推荐指数
1
解决办法
5740
查看次数

Grails项目运行错误-Server无法启动端口8080:已在使用的地址:JVM_Bind

错误| 服务器无法启动端口8080:地址已在使用中:JVM_Bind(使用--stacktrace查看完整跟踪)

当我第二次使用--- 2 Grails Command(run-app)执行我的grails项目时,每次都会遇到这种类型的错误.

但是当我使用任务管理器杀死javaw.exe时,如果我再次运行它对我有效.这有什么永久的解决方案吗?..请建议.

|包装Grails应用..

|编译10个源文件..

|编译121个源文件.......

|编译9个源文件.........................

|运行Grails应用程序

错误| 服务器无法启动端口8080:地址已在使用中:JVM_Bind(使用--stacktrace查看完整跟踪)

grails

2
推荐指数
1
解决办法
6730
查看次数

什么是空封闭的Groovy真理?

我试图在Groovy中预测此代码的行为

userList.find { }
Run Code Online (Sandbox Code Playgroud)

.find文档

find调用该方法并传递一个闭包时,它返回第一个评估闭包的元素,使其理解为Groovies true.当find没有任何参数调用该方法时,它返回列表中true根据Groovy真值匹配的第一个对象.

如果使用空闭包会发生什么?

  1. 它会评估为true,从而返回列表的第一个元素吗?
  2. 是否总是评估为false并且在迭代列表后null返回?
  3. 它会表现得像.find()

groovy closures

2
推荐指数
1
解决办法
2086
查看次数

Groovy函数调用省略括号

根据gradle文档/第13.5.2节,我们可以省略方法调用中的括号:

括号是方法调用的可选项.

但是当我们尝试应用java插件时它似乎不起作用.如果脚本包含以下行:

apply [plugin: 'java']
Run Code Online (Sandbox Code Playgroud)

我们会收到错误:

 Maybe something should be set in parentheses or a comma is missing?
 @ line 1, column 8.
     apply [plugin: 'java']
            ^
Run Code Online (Sandbox Code Playgroud)

但是如果我们把这个Map-literal放到括号中它就可以了.

apply([plugin: 'java'])
Run Code Online (Sandbox Code Playgroud)

因此,当参数为a时Map,我们不能省略括号,是吗?

groovy gradle

2
推荐指数
1
解决办法
648
查看次数

标签 统计

groovy ×7

grails ×3

closures ×1

gradle ×1

grails-2.0.4 ×1

json ×1

null ×1

spock ×1

testing ×1