简体   繁体   English

RestTemplate调用错误

[英]RestTemplate invocation error

I have postman body as attached in the image 我有邮递员的身体,如图所示 在此处输入图片说明

I am trying to call restTemplate as follows. 我试图按如下方式调用restTemplate。

String body= "client_id=admin-cli&username=abc&password=root&grant_type=password";
HttpHeaders headers = new HttpHeaders();
headers.setCacheControl("no-cache");
headers.set("content-type", "application/x-www-form-urlencoded");

HttpEntity<String> request = new HttpEntity<String>(body, headers);         
    ResponseEntity<String> response = new RestTemplate().postForEntity(url, request, String.class);

But seems like body should not be simple string (with "&" in between values, as I am doing now). 但是似乎body不应该是简单的字符串(在值之间使用“&”,就像我现在所做的那样)。 Any leads how to parse the body for the shown string in image? 任何线索都如何为图像中显示的字符串解析正文?

Here goes the stack trace: 这是堆栈跟踪: 在此处输入图片说明

java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/exc/InvalidDefinitionException
    at org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter.<init>(AllEncompassingFormHttpMessageConverter.java:74)
    at org.springframework.web.client.RestTemplate.<init>(RestTemplate.java:179)
    at io.opensaber.registry.authorization.AuthorizationFilterTest.test_valid_token(AuthorizationFilterTest.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:239)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.exc.InvalidDefinitionException
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 27 more

PS I found the issue and resolved. PS我发现了问题并解决了。 It was related to version incompatibility of jackson dependency. 它与杰克逊依赖项的版本不兼容有关。

You can put the params into the HttpEntity as a Map . 您可以将参数作为Map放入HttpEntity中。 See example below. 请参见下面的示例。

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.setCacheControl("no-cache");

MultiValueMap<String, String> params= new LinkedMultiValueMap<>();
params.add("client_id", "admin-cli");
params.add("username", "abc");
params.add("password", "root");
params.add("grant_type", "password");

HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, headers);
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM