Ratpack form parser

Ratpack extension for parsing requests

License

License

Categories

Categories

ORM Data Ratpack User Interface Web Frameworks
GroupId

GroupId

one.chest.ratpack
ArtifactId

ArtifactId

form-parser
Last Version

Last Version

0.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

Ratpack form parser
Ratpack extension for parsing requests
Source Code Management

Source Code Management

https://github.com/DNAlchemist/ratpack-form-parser

Download form-parser

How to add to project

<!-- https://jarcasting.com/artifacts/one.chest.ratpack/form-parser/ -->
<dependency>
    <groupId>one.chest.ratpack</groupId>
    <artifactId>form-parser</artifactId>
    <version>0.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/one.chest.ratpack/form-parser/
implementation 'one.chest.ratpack:form-parser:0.0.1'
// https://jarcasting.com/artifacts/one.chest.ratpack/form-parser/
implementation ("one.chest.ratpack:form-parser:0.0.1")
'one.chest.ratpack:form-parser:jar:0.0.1'
<dependency org="one.chest.ratpack" name="form-parser" rev="0.0.1">
  <artifact name="form-parser" type="jar" />
</dependency>
@Grapes(
@Grab(group='one.chest.ratpack', module='form-parser', version='0.0.1')
)
libraryDependencies += "one.chest.ratpack" % "form-parser" % "0.0.1"
[one.chest.ratpack/form-parser "0.0.1"]

Dependencies

compile (2)

Group / Artifact Type Version
org.codehaus.groovy : groovy-all jar 2.4.7
javax.validation : validation-api jar 1.1.0.Final

test (4)

Group / Artifact Type Version
io.ratpack : ratpack-groovy jar 1.4.6
org.glassfish.web : el-impl jar 2.2
org.hibernate : hibernate-validator-cdi jar 5.4.1.Final
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

Build Status Maven Central Coverage Status

License

Usage

Parse request into POGO object

// curl -XPOST localhost:5050/person --data "age=18"  

class Person {
    int age
}

chain.post("person", { Context ctx ->
    ctx.parse(Form) { Form form -> 
        Person person = form as Person
        assert person.age == 18
    }
})

Parse request into Java Object

// curl -XPOST localhost:5050/atomicinteger --data "value=42"

import java.util.concurrent.atomic.AtomicInteger
chain.post("atomicinteger", { Context ctx ->
    ctx.parse(Form) { Form form -> 
        AtomicInteger atomicInteger = form as AtomicInteger
        assert atomicInteger.get() == 42
    }
})

Parse using custom form property name

// curl -XPOST localhost:5050/balloon --data "mass_density=0.179"  

import one.chest.ratpack.form.FormProperty
class Balloon {
    @FormProperty("mass_density")
    float massDensity
}

chain.post("balloon", { Context ctx ->
    ctx.parse(Form) { Form form -> 
        Balloon balloon = form as Balloon
        assert balloon.massDesnity == 0.179
    }
})

Parse via set interception

// curl -XPOST localhost:5050/book \ 
// --data "author=Edgar Allan Poe" \
// --data "stories=The Gold-Bug, The Murders in the Rue Morgue, The Mystery of Marie Roget"  

class Book {
    String author
    List<String> stories
    def setStories(String commaSeparatedList) {
        stories = commaSeparatedList.split(",")*.trim()
    }
}

chain.post("book", { Context ctx ->
    ctx.parse(Form) { Form form -> 
        Book book = form as Book
        assert book.author == 'Edgar Allan Poe'
        assert book.stories == [
            'The Gold-Bug',
            'The Murders in the Rue Morgue', 
            'The Mystery of Marie Roget'
        ]
    }
})

Validation

To use validation features, you must include one of the libraries that implement JSR 303

// curl -XPOST localhost:5050/person --data "age=15"

class Person {
    @Min(value = 18, message = "You must be 18 years or older") 
    int age
}

chain.post("person", { Context ctx ->
    ctx.parse(Form) { Form form ->
        try {
            Person person = form as Person
        } catch(ValidationException e) {
            ctx.render e.message
        }
    }
})

See also

FormIgnoreProperties

Shadow Jar

If you use a shadow library, do not forget to merge the extension module

shadowJar {
    mergeGroovyExtensionModules()
} 

Versions

Version
0.0.1