我知道(相当)类似的问题被问了很多次,但这些问题的解决方案对我来说不起作用。
我正在使用 JAX-RS 和 Jersey 2 创建一个 REST 服务,它使用来自 post 请求的 json beans,它看起来像这样:
@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")
);
}
}
当我尝试将 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
关于java - JAX-RS 服务始终向 POST 请求回复 415 Unsupported Media Type 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30169299/