【问题标题】:Spring 5 RouterFunction status 415 with reason "Content type 'application/json' not supported"Spring 5 RouterFunction 状态 415,原因为“不支持内容类型‘application/json’”
【发布时间】:2017-10-22 23:22:33
【问题描述】:

我正在尝试按照以下代码使用 Spring 5 Rounder Function

public class PersonHandler {

    private final PersonRepository repository;

    public PersonHandler(PersonRepository repository) {
        this.repository = repository;
    }

    public Mono<ServerResponse> listPeople(ServerRequest request) { 
        System.out.println("List people");
        Flux<Person> people = repository.findAll();
        return ServerResponse.ok().contentType(APPLICATION_JSON).body(people, Person.class);
    }

    public Mono<ServerResponse> createPerson(ServerRequest request) { 
        System.out.println("Insert people");
        Mono<Person> person = request.bodyToMono(Person.class); 
        repository.insert(person).subscribe();
        return ServerResponse.ok().build();
    }

    public Mono<ServerResponse> getPerson(ServerRequest request) { 
        System.out.println("Get people");
        String personName = request.pathVariable("name");
        Mono<ServerResponse> notFound = ServerResponse.notFound().build();
        Mono<Person> personMono = this.repository.findByName(personName);
        return personMono
                .flatMap(person -> ServerResponse.ok().contentType(APPLICATION_JSON).body(fromObject(person)))
                .switchIfEmpty(notFound);
    }
}

这是路由功能

@Configuration
public class RoutesConfiguration {

    @Bean
    RouterFunction<?> routes(PersonRepository personRepository) {

        PersonHandler handler = new PersonHandler(personRepository);
        return nest(path("/person"),
                nest(accept(APPLICATION_JSON),
                        route(GET("/{id}"), handler::getPerson)
                        .andRoute(method(HttpMethod.GET), handler::listPeople)
                ).andRoute(POST("/").and(contentType(APPLICATION_JSON)), handler::createPerson));
    }
}

当我执行 curl 请求时

curl -X POST -d {"name":"John Doe","age":20} -H "Content-Type: application/json" http://localhost:8081/person

但是,我收到了 POST 请求的错误消息

    reactor.core.Exceptions$ErrorCallbackNotImplemented: org.springframework.web.server.UnsupportedMediaTypeStatusException: Response status 415 with reason "Con
tent type 'application/json' not supported"
Caused by: org.springframework.web.server.UnsupportedMediaTypeStatusException: Response status 415 with reason "Content type 'application/json' not supported
"
        at org.springframework.web.reactive.function.server.DefaultServerRequest.lambda$static$0(DefaultServerRequest.java:66) ~[spring-webflux-5.0.0.RELEASE
.jar:5.0.0.RELEASE]
        at reactor.core.publisher.Mono.lambda$onErrorMap$19(Mono.java:2354) ~[reactor-core-3.1.1.RELEASE.jar:3.1.1.RELEASE]
        at reactor.core.publisher.Mono.lambda$onErrorResume$21(Mono.java:2446) ~[reactor-core-3.1.1.RELEASE.jar:3.1.1.RELEASE]
        at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onError(FluxOnErrorResume.java:88) ~[reactor-core-3.1.1.RELEASE.jar:3.1.1.RELEASE]
        at reactor.core.publisher.Operators.error(Operators.java:175) ~[reactor-core-3.1.1.RELEASE.jar:3.1.1.RELEASE]
        at reactor.core.publisher.MonoError.subscribe(MonoError.java:52) ~[reactor-core-3.1.1.RELEASE.jar:3.1.1.RELEASE]

任何人都可以帮助解释这个错误的原因是什么?谢谢

【问题讨论】:

  • 您找到解决此问题的方法了吗?

标签: spring-webflux


【解决方案1】:

这件事发生在我身上。我使用 Maven 并通过清除我的 m2 缓存解决了这个问题:

rm -rf ~/.m2/repository/* (my .m2 folder is in my home directory)

然后我运行mvn clean install 并重新启动我的应用程序,但没有收到异常。

【讨论】:

    猜你喜欢
    • 2017-10-21
    • 2016-11-17
    • 2022-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 2019-02-20
    • 2018-08-06
    相关资源
    最近更新 更多