【问题标题】:Spring boot with restful services带有宁静服务的 Spring Boot
【发布时间】:2015-07-19 05:24:06
【问题描述】:

我是Spring boot 的新手。

我正在尝试编写一个使用发布请求作为输入的代码。

当我尝试通过 post rest 客户端 chrome 插件发送 post 请求时出现此错误。

我正在使用 Maven 构建工具

{
    "timestamp": 1431079188726,
    "status": 415,
    "error": "Unsupported Media Type",
    "exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
    "message": "Content type 'text/plain;charset=UTF-8' not supported",
    "path": "/api/greetings" }

这些是类。

Application.java

@SpringBootApplication
@ComponentScan(basePackages="webapi")
public class Application {


    public static void main(String[] args) throws Exception{
        SpringApplication.run(Application.class, args);

    }

}

Greeting.java

public class Greeting {

    private BigInteger id;
    private String text;

    public Greeting() {

    }

    public BigInteger getId() {
        return id;
    }

    public void setId(BigInteger id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

}

GreetingController.java

@RestController

public class GreetingController {



    private static BigInteger nextId;
    private static Map<BigInteger, Greeting> greetingMap;


    private static Greeting save(Greeting greeting) {
        if (greetingMap == null) {
            greetingMap = new HashMap<BigInteger, Greeting>();
            nextId = BigInteger.ONE;

        }
        greeting.setId(nextId);
        nextId = nextId.add(BigInteger.ONE);
        greetingMap.put(greeting.getId(), greeting);
        return greeting;
    }

    static {
        Greeting g1 = new Greeting();
        g1.setText("Hello World");
        save(g1);

        Greeting g2 = new Greeting();
        g2.setText("Hola Mundo!");
        save(g2);

    }



    @RequestMapping(value = "/api/greetings", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)

    public ResponseEntity<Collection<Greeting>> getGreetings() {
        Collection<Greeting> greetings = greetingMap.values();

        return new ResponseEntity<Collection<Greeting>>(greetings,
                HttpStatus.OK);
    }


    @RequestMapping(value = "/api/greetings/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)

    public ResponseEntity<Greeting> getGreeting(
            @PathVariable("id") BigInteger id)
    {
        Greeting greeting = greetingMap.get(id);
        if (greeting == null) {
            return new ResponseEntity<Greeting>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<Greeting>(greeting, HttpStatus.OK);

    }


    @RequestMapping(value = "/api/greetings", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResponseEntity<Greeting> createGreeting(
            @RequestBody Greeting greeting) {
        Greeting savedGreeting = save(greeting);
        return new ResponseEntity<Greeting>(savedGreeting, HttpStatus.CREATED);
    }

}

【问题讨论】:

    标签: java spring rest maven spring-mvc


    【解决方案1】:

    该服务期待 application/json Content-Type 但它接收到 text/plain 输入。

    你用什么来发帖?如果您使用 Postman 进行测试,请务必在标题部分添加 Content-Typeapplication/json 值。

    【讨论】:

      【解决方案2】:

      您已将路径 /api/greetings(带有参数)的映射定义为 consumes = MediaType.APPLICATION_JSON_VALUE,因此编译器正在等待 json 输入,但它正在获取类型为 text/plain(它在堆栈跟踪中提到)的请求,因此无论您来自何处正在传递请求,确保内容类型为“application/json”。

      因为/api/greetings 类型text/plain 没有映射,所以抛出此异常!

      希望对你有帮助!

      祝你好运!

      【讨论】:

      • @KunalMalhotra 有帮助吗?
      猜你喜欢
      • 2019-03-17
      • 2018-04-21
      • 2019-04-15
      • 2011-12-10
      • 2013-12-07
      • 2016-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多