2015-08-15 3 views
-1

Этот пример работает для кодированного URL GET! Я пытался улучшить его с помощью JSON POST, который не работает. Может ли кто-нибудь помочь мне улучшить его с помощью JSON POST, DELETE и JSON PUT и некоторых сопровождающих команд curl в комментариях, чтобы продемонстрировать эти команды?Groovy Spring Boot: как сделать JSON POST, PUT, DELETE?

/* 
* Begin commands to execute this file using Groovy with bash 
* spring run app.groovy & 
* sleep 10 
* curl -i -H "Accept: application/json" http://localhost:8080/greeting?name=siegfried -X GET 
* End commands to execute this file using Groovy with bash 
* 
* 
* $Log$ 
* 
*/ 


import java.util.concurrent.atomic.AtomicLong; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestBody; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.RestController; 

public class Greeting { 

    public final long id; 
    public final String content; 

    public Greeting(){ this(0,"") } 
    public Greeting(long id, String content) { 
     this.id = id; 
     this.content = content; 
    } 
} 

@RestController 
class ThisWillActuallyRun { 

    @RequestMapping("/") 
    String home() { 
     "Hello World! " + new java.util.Date() 
    } 
    private static final String template = "Hello, %s!"; 
    private final AtomicLong counter = new AtomicLong(); 

    @RequestMapping("/greeting") 
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) { 
     return new Greeting(counter.incrementAndGet(), String.format(template, name)); 
    } 

    // {"id":1,"content":"Hello, siegfried!"} 

    //@RequestMapping(value="/g2", method=RequestMethod.GET) public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) { return new Greeting(counter.incrementAndGet(), String.format("Hello2, %s!", name)); } 
    //@RequestMapping(value="/g3", method=RequestMethod.POST, headers = "Content-Type=application/json") public Greeting greeting(@RequestBody Greeting g) { return new Greeting(counter.incrementAndGet(), String.format("Hello2, %s!", g.content)); } 
    //{"timestamp":1439600325530,"status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content type 'application/x-www-form-urlencoded' not supported","path":"/g3"} 

} 
+0

Вы пробовали также аннотирования свои методы с '@ Post' (а также ваш' @ RequestMapping' аннотаций)? –

ответ

1

Попробуй как в этом блоге: http://ryanjbaxter.com/2014/12/17/building-rest-apis-with-spring-boot/

import java.util.List; 
import java.util.ArrayList; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestBody; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RestController; 

@RestController 
@RequestMapping("/contacts") 
public class ContactRestController { 

@RequestMapping(method=RequestMethod.GET) 
    public List<Contact> getAll() { 
    return new ArrayList<Contact>(); 
    } 

    @RequestMapping(method=RequestMethod.POST) 
    public Contact create(@RequestBody Contact contact) { 
    return null; 
    } 

    @RequestMapping(method=RequestMethod.DELETE, value="{id}") 
    public void delete(@PathVariable String id) { 

    } 

    @RequestMapping(method=RequestMethod.PUT, value="{id}") 
    public Contact update(@PathVariable String id, @RequestBody Contact contact) { 
    return null; 
    } 

} 
Смежные вопросы