JAX-RS 服务总是以 415 Unsupported Media Type 错误回复 POST 请求

JAX-RS service always replies with 415 Unsupported Media Type error to POST requests

我知道(相当)类似的问题被问过很多次,但它们的解决方案对我不起作用。

我正在使用 JAX-RS 和 Jersey 2 创建一个 REST 服务,它使用来自 post 个请求的 json 个 bean,它看起来像这样:

@Path("auth")
public class AuthService {
@Inject
private AuthenticationManager authenticationManager;

@POST
@Path("login")
@Consumes(MediaType.APPLICATION_JSON)
public Response login(ApiCredentials credentials){
    try {
        authenticationManager.login(credentials);
        return Response.ok().build();
    } catch (Exception e){
        e.printStackTrace();
        return Response.status(Response.Status.UNAUTHORIZED).build();
    }
}

并在以编程方式配置的嵌入式 tomcat

中运行
public class WebServer {
public static void main(String[] args) throws ServletException, LifecycleException, MalformedURLException{
    String webappDirLocation = "webapp/";
    Tomcat tomcat = new Tomcat();
    // Define port number for the web application
    String webPort = System.getenv("PORT");
    if (webPort == null || webPort.isEmpty()) {
        webPort = "8089";
    }
    // Bind the port to Tomcat server
    tomcat.setPort(Integer.valueOf(webPort));

    // Define a web application context.
    Context context = tomcat.addWebapp("/tomcatembedded", new File(
            webappDirLocation).getAbsolutePath());

    Tomcat.addServlet(context, "jersey-container-servlet", resourceConfig());
    context.addServletMapping("/rest/*", "jersey-container-servlet");


    tomcat.start();
    tomcat.getServer().await();
}
private static ServletContainer resourceConfig(){
    return new ServletContainer(
            new ResourceConfig(
                new ResourceLoader().getClasses()
            )
            .register(new ApplicationBinder()) //binds AuthenticationManager
            .packages("com.dockerhosting.api.rest.auth", "com.dockerhosting.rest.auth")
    );
}
}

当我尝试 post 一个 json 序列化的 ApiCredentials 到它回复的其余服务时

<!DOCTYPE html><html><head><title>Apache Tomcat/8.0.22 - Error report</title><style type="text/css">H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}.line {height: 1px; background-color: #525D76; border: none;}</style> </head><body><h1>HTTP Status 415 - Unsupported Media Type</h1><div class="line"></div><p><b>type</b> Status report</p><p><b>message</b> <u>Unsupported Media Type</u></p><p><b>description</b> <u>The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.</u></p><hr class="line"><h3>Apache Tomcat/8.0.22</h3></body></html>

我尝试将 mimepull 添加到服务的类路径,并将 Content-Type header 添加到客户端,但没有任何改变。客户端代码为:

JerseyClient client = new JerseyClientBuilder().build();

    JerseyWebTarget target = client.target("http://192.168.122.156:8089/tomcatembedded/rest/auth/login");
    Response response = target.request()
            .header("Content-Type", "application/json;charset=UTF-8")
            .accept(MediaType.APPLICATION_JSON)
            .post(Entity.entity(credentials, MediaType.APPLICATION_JSON));
    String output = response.readEntity(String.class);
    System.out.println(output);

有什么想法吗?

谢谢

来自评论(只是为了得到一个答案......见评论讨论)

Artem Zhirkov: I explicitely registered JacksonJsonProvider with ResourceConfig and it solved the problem