Pipeline call failed using Rest Template - java

I have a scenario where I want to pause a pineline using my spring-boot service. I am using Spring-boot and Go Pipeline API 16.1.0
Following is actual url available in GO API documentation to pause pipeline through CURL which is working perfectly
curl 'http://ci.example.com/go/api/pipelines/dev1-pineline/pause' -u
'username:password' -X POST -d 'pauseCause=testing Pause'
Above CURL URL working as expected but when I try to do same with RestTemplate it's not working.
RestTemplate function code is
public Object pausePipeline() {
String credentials = userName+":"+password;
byte[] encoding = Base64.encodeBase64(credentials.getBytes());
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", String.valueOf(encoding));
//headers.setContentType(MediaType.APPLICATION_JSON); // optional
String url = String.format("%s/%s%s", pipelineUrl,pipelineName,"/pause");
String data = "pauseCause=Monthend process started.";
HttpEntity<String> entity = new HttpEntity<>(data, headers);
return restTemplate.exchange(url, HttpMethod.POST, entity , Object.class).getBody();
}
Above code throws an exception when try to connect using Rest endpoint Exception stacktrace is
[o.a.c.c.C.[.[.[.[dispatcherServlet]] (http-nio-8080-exec-1) Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException: 401 Full authentication is required to access this resource] with root cause
org.springframework.web.client.HttpClientErrorException: 401 Full authentication is required to access this resource
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91) ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:614) ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:570) ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:530) ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:448) ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at com.premierinc.forecasting.suggesteds.MonthEndService.pausePipeline(MonthEndService.java:51) ~[main/:?]
at com.premierinc.forecasting.web.ExtractsApi.pauseMonendPipeline(ExtractsApi.java:104) ~[main/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_181]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_181]
at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1270) ~[springloaded-1.2.3.RELEASE.jar:1.2.3.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) ~[seb-4.1.6.RELEASE.jar:4.1.6.RELEASE]

You might want to prepend Basic to your Authorization header. The relevant change in your code might be similar to the one below
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Basic " + String.valueOf(encoding));
Reference - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization#Examples

Related

Using keycloak api for a public client

Working on a project where there is a frontend (React) and a backend (Java/Quarkus). Keycloak is used as the auth tool. It is configured with a realm that has a public client on it. This allows the user to login via the browser and then make api requests to the backend with a bearer token attached to the request.
Running into a problem where we want to add users to our application. This means we also need to add them to keycloak. Doing this programatically, this is in its own mini module and it uses the keycloak-admin-client library.
I initiate the instance with
instance = KeycloakBuilder.builder()
.serverUrl(props.getProperty(PropertyConstants.SERVER_URL))
.realm(props.getProperty(PropertyConstants.REALM))
.grantType(OAuth2Constants.PASSWORD)
.clientId(props.getProperty(PropertyConstants.CLIENT_ID))
.username(props.getProperty(PropertyConstants.USERNAME))
.password(props.getProperty(PropertyConstants.PASSWORD))
.build();
realmResource = instance.realm(props.getProperty(PropertyConstants.REALM));
usersResource = realmResource.users();
UserRepresentation user = createUserRep();
Response response = usersResource.create(user);
The stacktrace is;
org.jboss.resteasy.spi.UnhandledException: javax.ws.rs.ProcessingException: RESTEASY004655: Unable to invoke request: javax.ws.rs.ProcessingException: RESTEASY003215: could not find writer for content-type application/x-www-form-urlencoded type: javax.ws.rs.core.Form$1
at org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:106)
at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:372)
at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:218)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:519)
at org.jboss.resteasy.core.SynchronousDispatcher.lambda$invoke$4(SynchronousDispatcher.java:261)
at org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess$0(SynchronousDispatcher.java:161)
at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:364)
at org.jboss.resteasy.core.SynchronousDispatcher.preprocess(SynchronousDispatcher.java:164)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:247)
at io.quarkus.resteasy.runtime.standalone.RequestDispatcher.service(RequestDispatcher.java:73)
at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler.dispatch(VertxRequestHandler.java:136)
at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler.access$000(VertxRequestHandler.java:40)
at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler$1.run(VertxRequestHandler.java:97)
at io.quarkus.runtime.CleanableExecutor$CleaningRunnable.run(CleanableExecutor.java:231)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:2046)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1578)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1452)
at org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)
at org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)
at java.base/java.lang.Thread.run(Thread.java:829)
at org.jboss.threads.JBossThread.run(JBossThread.java:479)
Caused by: javax.ws.rs.ProcessingException: RESTEASY004655: Unable to invoke request: javax.ws.rs.ProcessingException: RESTEASY003215: could not find writer for content-type application/x-www-form-urlencoded type: javax.ws.rs.core.Form$1
at org.jboss.resteasy.client.jaxrs.engines.ManualClosingApacheHttpClient43Engine.invoke(ManualClosingApacheHttpClient43Engine.java:287)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:488)
at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invokeSync(ClientInvoker.java:149)
at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:112)
at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76)
at com.sun.proxy.$Proxy89.grantToken(Unknown Source)
at org.keycloak.admin.client.token.TokenManager.grantToken(TokenManager.java:90)
at org.keycloak.admin.client.token.TokenManager.getAccessToken(TokenManager.java:70)
at org.keycloak.admin.client.token.TokenManager.getAccessTokenString(TokenManager.java:65)
at org.keycloak.admin.client.resource.BearerAuthFilter.filter(BearerAuthFilter.java:52)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.filterRequest(ClientInvocation.java:683)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:485)
at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invokeSync(ClientInvoker.java:149)
at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:112)
at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76)
at com.sun.proxy.$Proxy96.create(Unknown Source)
at com.demservices.KeycloakWrapper.addUser(KeycloakWrapper.java:56)
at com.demservices.services.UserService.addUser(UserService.java:27)
at com.demservices.services.UserService_Subclass.addUser$$superaccessor2(UserService_Subclass.zig:492)
at com.demservices.services.UserService_Subclass$$function$$2.apply(UserService_Subclass$$function$$2.zig:33)
at io.quarkus.arc.impl.AroundInvokeInvocationContext.proceed(AroundInvokeInvocationContext.java:54)
at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorBase.invokeInOurTx(TransactionalInterceptorBase.java:127)
at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorBase.invokeInOurTx(TransactionalInterceptorBase.java:100)
at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorRequired.doIntercept(TransactionalInterceptorRequired.java:32)
at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorBase.intercept(TransactionalInterceptorBase.java:53)
at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorRequired.intercept(TransactionalInterceptorRequired.java:26)
at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorRequired_Bean.intercept(TransactionalInterceptorRequired_Bean.zig:340)
at io.quarkus.arc.impl.InterceptorInvocation.invoke(InterceptorInvocation.java:41)
at io.quarkus.arc.impl.AroundInvokeInvocationContext.perform(AroundInvokeInvocationContext.java:41)
at io.quarkus.arc.impl.InvocationContexts.performAroundInvoke(InvocationContexts.java:32)
at com.demservices.services.UserService_Subclass.addUser(UserService_Subclass.zig:440)
at com.demservices.services.UserService_ClientProxy.addUser(UserService_ClientProxy.zig:251)
at com.demservices.controllers.UserController.createNewUser(UserController.java:35)
at com.demservices.controllers.UserController_Subclass.createNewUser$$superaccessor1(UserController_Subclass.zig:234)
at com.demservices.controllers.UserController_Subclass$$function$$1.apply(UserController_Subclass$$function$$1.zig:33)
at io.quarkus.arc.impl.AroundInvokeInvocationContext.proceed(AroundInvokeInvocationContext.java:54)
at io.quarkus.hibernate.validator.runtime.interceptor.AbstractMethodValidationInterceptor.validateMethodInvocation(AbstractMethodValidationInterceptor.java:69)
at io.quarkus.hibernate.validator.runtime.jaxrs.JaxrsEndPointValidationInterceptor.validateMethodInvocation(JaxrsEndPointValidationInterceptor.java:35)
at io.quarkus.hibernate.validator.runtime.jaxrs.JaxrsEndPointValidationInterceptor_Bean.intercept(JaxrsEndPointValidationInterceptor_Bean.zig:392)
at io.quarkus.arc.impl.InterceptorInvocation.invoke(InterceptorInvocation.java:41)
at io.quarkus.arc.impl.AroundInvokeInvocationContext.perform(AroundInvokeInvocationContext.java:41)
at io.quarkus.arc.impl.InvocationContexts.performAroundInvoke(InvocationContexts.java:32)
at com.demservices.controllers.UserController_Subclass.createNewUser(UserController_Subclass.zig:191)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:170)
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:130)
at org.jboss.resteasy.core.ResourceMethodInvoker.internalInvokeOnTarget(ResourceMethodInvoker.java:643)
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTargetAfterFilter(ResourceMethodInvoker.java:507)
at org.jboss.resteasy.core.ResourceMethodInvoker.lambda$invokeOnTarget$2(ResourceMethodInvoker.java:457)
at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:364)
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:459)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:419)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:393)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:68)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:492)
... 20 more
Caused by: javax.ws.rs.ProcessingException: RESTEASY003215: could not find writer for content-type application/x-www-form-urlencoded type: javax.ws.rs.core.Form$1
at org.jboss.resteasy.core.interception.jaxrs.ClientWriterInterceptorContext.throwWriterNotFoundException(ClientWriterInterceptorContext.java:50)
at org.jboss.resteasy.core.interception.jaxrs.AbstractWriterInterceptorContext.getWriter(AbstractWriterInterceptorContext.java:302)
at org.jboss.resteasy.core.interception.jaxrs.AbstractWriterInterceptorContext.syncProceed(AbstractWriterInterceptorContext.java:240)
at org.jboss.resteasy.core.interception.jaxrs.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:224)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.writeRequestBody(ClientInvocation.java:440)
at org.jboss.resteasy.client.jaxrs.engines.ManualClosingApacheHttpClient43Engine.writeRequestBodyToOutputStream(ManualClosingApacheHttpClient43Engine.java:589)
at org.jboss.resteasy.client.jaxrs.engines.ManualClosingApacheHttpClient43Engine.buildEntity(ManualClosingApacheHttpClient43Engine.java:548)
at org.jboss.resteasy.client.jaxrs.engines.ManualClosingApacheHttpClient43Engine.loadHttpMethod(ManualClosingApacheHttpClient43Engine.java:455)
at org.jboss.resteasy.client.jaxrs.engines.ManualClosingApacheHttpClient43Engine.invoke(ManualClosingApacheHttpClient43Engine.java:265)
... 77 more
I see in any example that a clientSecret is required but that is for confidential clients. I don't want this, I need the client to be public.
How can I have a public client while also being able to make API calls to it?

http java call which goes wrong

I have a java application on Weblogic 12, i make an https call to a SAP server in REST (GET e basic authentication), everything works for 30/45 minutes but after i have "503: Service Unavailable". If I call this service from browser it always works.
HttpURLConnection conn = (HttpURLConnection) new URL( url.toString()).openConnection();
conn.setRequestProperty("Authorization", "Basic " + authUserEnc);
... //read the response stream
conn.disconnect();
this is the error
java.io.FileNotFoundException: Response: '503: Service Unavailable' for url: 'https://fwp-as3.enelint.global:44331/sap/opu/odata/sap/ZFIOI_O1_Q006_SRV/ZFIOI_O1_Q006(ZVAR_VDMCUI_OI_M='0000022077',ZVAR_VDMCUI_OI_MTo='0000022077')/Results?saml2=disabled'
at weblogic.net.http.HttpURLConnection.getInputStream(HttpURLConnection.java:829)
at weblogic.net.http.SOAPHttpsURLConnection.getInputStream(SOAPHttpsURLConnection.java:42)
at it.exprivia.bexboard.service.impl.VendorServiceImpl.getData(VendorServiceImpl.java:125)
at it.exprivia.bexboard.controller.GrafxController.getVendorGrafxById(GrafxController.java:166)
at sun.reflect.GeneratedMethodAccessor211.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:181)
at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:97)
at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:202)
at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:101)
at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:59)
at ...

Spring Neo4j - Cannot access unmanaged extension

I've deployed a Neo4j unmanaged extension. The unmanaged extension can be called using REST Client and successfully returned the result. The problem is when I try to call / invoke the unmanaged extension from another java class, it keep on throwing the 401 Unauthorized.
I used Spring RestTemplate to invoke the unmanaged extension.
My codes :
RestTemplate restTemplate = new RestTemplate();
PostPhotoRest postPhotoRest = restTemplate.getForObject("http://myneo4jusername:myneo4jpassword#localhost:7474/extension/servicetwo/postphoto/55b12d35-94fd-4297-bb18-e6040d7b7109", PostPhotoRest.class);
Full Error :
Caused by: org.springframework.web.client.HttpClientErrorException: 401 Unauthorized
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91) ~[spring-web-4.3.0.RC2.jar:4.3.0.RC2]
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:668) ~[spring-web-4.3.0.RC2.jar:4.3.0.RC2]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:621) ~[spring-web-4.3.0.RC2.jar:4.3.0.RC2]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:581) ~[spring-web-4.3.0.RC2.jar:4.3.0.RC2]
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:288) ~[spring-web-4.3.0.RC2.jar:4.3.0.RC2]
at my.winapp.hashtagmanipulation.worker.RabbitMQWorker.processMessage(RabbitMQWorker.java:76) ~[classes/:na]
at sun.reflect.GeneratedMethodAccessor27.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_66]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_66]
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197) ~[spring-messaging-4.3.0.RC2.jar:4.3.0.RC2]
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:115) ~[spring-messaging-4.3.0.RC2.jar:4.3.0.RC2]
at org.springframework.amqp.rabbit.listener.adapter.HandlerAdapter.invoke(HandlerAdapter.java:48) ~[spring-rabbit-1.5.5.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:112) ~[spring-rabbit-1.5.5.RELEASE.jar:na]
... 12 common frames omitted
The driver configuration can supply the URI and credentials configured in ogm.properties.
Components.driver().getConfiguration().getURI();
Components.driver().getConfiguration().getCredentials();

Problems with Spring RestTemplate Post for Object with String

I've been struggling to solve this issue here, and i can't find what i am doing wrong.
I am trying to make a simple JSON Post, using Spring RestTemplate, and sending all information as String to be simple all the way around as follows:
RestTemplate restTemplate = new RestTemplate();
JSONObject issue = getJSONIssue(redmine);
try {
String response = restTemplate.postForObject(redmineProperties.getUrl()+"/issues.json?key="+redmineProperties.getKey(), issue.toJSONString(), String.class);
}catch(RestClientException e){
e.printStackTrace();
}
My JSONObject looks like:
{"issue":
{"priority_id":4,
"description":"asdasdasdasd",
"subject":"adasdad",
"project_id":4
}
}
This is the data i am trying to sendo via Post, and the server keep sending me the error:
[http-bio-8080-exec-7] WARN org.springframework.web.client.RestTemplate - POST request for "http://xxx.com.br/issues.json?key=b8143da980578fee4db0b33bd3cd3eec511797d6" resulted in 422 (Unprocessable Entity); invoking error handler
org.springframework.web.client.HttpClientErrorException: 422 Unprocessable Entity
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:589)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:547)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:503)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:331)
at br.com.cubbes.docmidia.controller.redmine.RedmineController.saveUser(RedmineController.java:101)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
when i send the same data via a client like insomnia by Google Chrome, the servers resolves without a problem.
Do anyone know what i could be doing wrong here?
For anyone coming here for 422 Unprocessable Entity, as suggested in one of the comments, setting the content-type:application/json fixed the error for me.
RequestEntity<ValidationRequest> requestEntity = RequestEntity.post(validatorUri)
.contentType(MediaType.APPLICATION_JSON)
.body(validationRequest);
JsonNode validationResponse = restTemplate.exchange(requestEntity, JsonNode.class).getBody();
I ended up using a specific library to manipulate this REST calls:
http://www.redmine.org/projects/redmine/wiki/Rest_api_with_java
Thanks guys

File Upload Failure by Jersey 2.0 Client

I'm trying to implement REST Client for Dropbox with Jersey 2.0.
In order to upload files, I use following code:
WebTarget target = client.target(targetUrl);
final FileDataBodyPart filePart = new FileDataBodyPart("file", file, MediaType.APPLICATION_OCTET_STREAM_TYPE);
final MultiPart multipart = new FormDataMultiPart().bodyPart(filePart);
Response response = target.request(MediaType.APPLICATION_JSON_TYPE).put(Entity.entity(multipart, multipart.getMediaType()));
However, this code does not work and MessageBodyProviderNotFoundException occurs in the put method.
How can I avoid the MessageBodyProviderNotFoundException in the put method.
Here's stack trace when the exception occurred:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
at java.lang.Thread.run(Thread.java:724)
Caused by: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=multipart/form-data, type=class org.glassfish.jersey.media.multipart.FormDataMultiPart, genericType=class org.glassfish.jersey.media.multipart.FormDataMultiPart.
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:191)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:139)
at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1005)
at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:430)
at org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:287)
at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:200)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:215)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:634)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:631)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:421)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:631)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:391)
at org.glassfish.jersey.client.JerseyInvocation$Builder.put(JerseyInvocation.java:285)
at com.clouddrive.dropbox.DropBoxController.uploadFile(DropBoxController.java:161)
at com.clouddrive.util.WatchDir.processEvents(WatchDir.java:200)
at com.clouddrive.CloudDrive.main(CloudDrive.java:111)
... 6 more
I modified initialization code and confirmed that the code work correctly.
Client client = ClientBuilder.newBuilder()
.register(MultiPartFeature.class)
.build();
I apologize for the inconvenience.

Categories