Spring MVC Quarkus代码不适用于HttpHeaders

tag5nh1u  于 2022-11-14  发布在  Spring
关注(0)|答案(1)|浏览(132)

这是我通过修改quarkus-quickstarts-main得到的代码:

package org.acme.spring.web;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@RestController
public class GreetingController {
    @PostMapping("/post")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response allParams(String smell,
                              @RequestHeader HttpHeaders headers) {
        return Response.ok("OK").build();
    }
}

Exception during startup:

    Caused by: java.lang.RuntimeException: Failed to process method 'org.acme.spring.web.GreetingController#allParams'
        at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.createResourceMethod(EndpointIndexer.java:703)
        at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.createEndpoints(EndpointIndexer.java:405)
        at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.createEndpoints(EndpointIndexer.java:286)
        ... 12 more
    Caused by: java.lang.RuntimeException: Could not create converter for org.springframework.http.HttpHeaders for method javax.ws.rs.core.Response allParams(java.lang.String smell, org.springframework.http.HttpHeaders headers) on class org.acme.spring.web.GreetingController of type QUERY
        at org.jboss.resteasy.reactive.server.processor.ServerEndpointIndexer.handleOtherParam(ServerEndpointIndexer.java:327)
        at org.jboss.resteasy.reactive.server.processor.ServerEndpointIndexer.handleOtherParam(ServerEndpointIndexer.java:92)
        at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.extractParameterInfo(EndpointIndexer.java:1335)
        at org.jboss.resteasy.reactive.common.processor.EndpointIndexer.createResourceMethod(EndpointIndexer.java:536)
        ... 14 more
    Caused by: java.lang.RuntimeException: Failed to find converter for org.springframework.http.HttpHeaders
        at org.jboss.resteasy.reactive.server.processor.generation.converters.GeneratedConverterIndexerExtension.extractConverterImpl(GeneratedConverterIndexerExtension.java:106)
        at org.jboss.resteasy.reactive.server.processor.ServerEndpointIndexer.extractConverter(ServerEndpointIndexer.java:513)
        at org.jboss.resteasy.reactive.server.processor.ServerEndpointIndexer.handleOtherParam(ServerEndpointIndexer.java:323)
        ... 17 more

Can someone help how to make the HttpHeaders work?

I tried several alternatives but not working.
ctehm74n

ctehm74n1#

您正在混合JAX-RS和Spring Web类型,这是不可行的。
您应该改为执行以下操作:

import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("")
public class GreetingController {
    @PostMapping("/post")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response allParams(String smell,
                              @Context HttpHeaders headers) {
        return Response.ok("OK").build();
    }
}

相关问题