java - JAX-RS service always replies with 415 Unsupported Media Type error to POST requests -
i know (quite) similar questions asked numerous times, solutions them didn't work me.
i'm creating rest service jax-rs , jersey 2 consumes json beans post requests, looks this:
@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(); } } and runs in programmatically configured embedded tomcat
public class webserver { public static void main(string[] args) throws servletexception, lifecycleexception, malformedurlexception{ string webappdirlocation = "webapp/"; tomcat tomcat = new tomcat(); // define port number web application string webport = system.getenv("port"); if (webport == null || webport.isempty()) { webport = "8089"; } // bind port tomcat server tomcat.setport(integer.valueof(webport)); // define 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") ); } } when trying post json serialized apicredentials rest service replies
<!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 request because request entity in format not supported requested resource requested method.</u></p><hr class="line"><h3>apache tomcat/8.0.22</h3></body></html> i tried add mimepull classpath of service , add content-type header client, didn't change anything. clinet code is:
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); any ideas?
thanks
from comment (just have answer... see comments discussion)
artem zhirkov: explicitely registered
jacksonjsonproviderresourceconfig , solved problem
Comments
Post a Comment