Visar inlägg med etikett H2. Visa alla inlägg
Visar inlägg med etikett H2. Visa alla inlägg

onsdag 17 september 2014

RestService with Springboot and Groovy

To test this you have to install GVM follow the instruction for your environment, I use Cygwin and GVM, when you have GVM up and running, install Springboot on GVM:
gvm install springboot

       
//Place this groovy-file in /cygwin/home/your_username
//To kick on the service open cygwin or another terminal and write: 

//spring run your_file_name.groovy
//Now the service is started, open a browser and test the service, f.e: http://localhost:8080/greeting/John
@Grab("org.grails:gorm-hibernate4-spring-boot:1.0.0.RC1") @Grab("com.h2database:h2:1.3.173") import grails.persistence.* import org.springframework.http.* import org.springframework.web.bind.annotation.* import static org.springframework.web.bind.annotation.RequestMethod.* @RestController class Application { @RequestMapping("/greeting") public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name) { return "greeting "+name } @RequestMapping("/greeting/{name}") public String greeting2(@PathVariable("name") String name) { return "greeting "+name } @RequestMapping(value = '/person/add/{birthDate}/{firstName}/{lastName}', method = GET) ResponseEntity addPerson(@PathVariable("birthDate") String birthDate, @PathVariable("firstName") String firstName, @PathVariable("lastName") String lastName) { Person.withTransaction { def p = new Person(firstName: firstName, lastName:lastName,birthDate:birthDate).save() if(p) { return new ResponseEntity("OK", HttpStatus.CREATED ) } else { return new ResponseEntity("BAD",HttpStatus.BAD_REQUEST) } } } @RequestMapping(value="/person/findByFirstName/{lang}/{value}", method = GET) String findByFirstName(@PathVariable("lang") String lang, @PathVariable("value") String value) { def salutation = getSalutation(lang) def p = Person.findByFirstName(value) return p ? "${salutation} ${p.firstName} ${p.lastName} !" : "Person not found" } private String getSalutation(String lang) { def salutation = 'Hello' if(lang.equals("sv")) { salutation = 'Hej' } else if (lang.equals("it")) { salutation = 'Salve' } return salutation } } @Entity class Person { String firstName String lastName String birthDate }