【问题标题】:Groovy Spring Boot: how to do JSON POST, PUT, DELETE?Groovy Spring Boot:如何执行 JSON POST、PUT、DELETE?
【发布时间】:2015-11-08 09:14:13
【问题描述】:

此示例适用于 URL 编码的 GET! 我尝试使用不起作用的 JSON POST 来增强它。有人可以帮助我使用 JSON POST、DELETE 和 JSON PUT 以及 cmets 中的一些随附 curl 命令来演示这些命令吗?

/*
 * 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"}

}

【问题讨论】:

  • 您是否尝试过使用@Post(以及您的@RequestMapping 注释)来注释您的方法?

标签: json rest groovy spring-boot


【解决方案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;
  }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-15
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    • 2021-08-09
    • 2018-08-01
    • 1970-01-01
    相关资源
    最近更新 更多