没有合适的HttpMessageConverter错误使我发疯

时间:2019-05-27 07:07:09

标签: java rest spring-boot jackson

我正在研究带有Spring Boot和Hibernate的静态API。 因此,我为登录做了简单的Restful API。 但是,会发生RestClientException错误。

我在StackOverflow中找到了一些解决方案。一个是在restTemplate中创建HttpMessageConverter。另一个是在配置类文件中为HttpMessageConverter进行配置。

这是我的测试代码。

@Test
    public void createDataset() {
//      List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
//      // Add the Jackson Message converter
//      MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
//      // Note: here we are making this converter to process any kind of response,
//      // not only application/*json, which is the default behaviour
//      converter.setSupportedMediaTypes(Collections.singletonList(
////                MediaType.ALL
////                MediaType.APPLICATION_JSON_UTF8
////                MediaType.APPLICATION_ATOM_XML
////                MediaType.APPLICATION_FORM_URLENCODED 
////                MediaType.APPLICATION_JSON
////                MediaType.APPLICATION_OCTET_STREAM 
////                MediaType.APPLICATION_PDF
////                MediaType.APPLICATION_PROBLEM_JSON
////                MediaType.APPLICATION_PROBLEM_JSON_UTF8 
////                MediaType.APPLICATION_PROBLEM_XML
////                MediaType.APPLICATION_RSS_XML
//              MediaType.APPLICATION_STREAM_JSON 
////                MediaType.APPLICATION_XHTML_XML
//              ));
//      messageConverters.add(converter);
//      restTemplate.getRestTemplate().setMessageConverters(messageConverters);

        Site site1 = new Site();
        site1.setSiteURL("www.t3q.com");
        Site site2 = new Site();
        site2.setSiteURL("www.t3q1.com");

        User user1 = new User();
        user1.setUsername("1111");
        user1.setPassword("12131");

        user1.addSite(site1);
        user1.addSite(site2);

//      site1.setUser(user1);
//      site2.setUser(user1);

        User user2 = new User();
        user2.setUsername("2222");
        user2.setPassword("1241");

        Site site3 = new Site();
        site3.setSiteURL("www.t3q.com");

        user2.addSite(site3);

//      site3.setUser(user2);

        restTemplate.postForEntity(getRootUrl() + "/users", user1, User.class);
        restTemplate.postForEntity(getRootUrl() + "/users", user2, User.class);

//      restTemplate.postForEntity(getRootUrl() + "/sites", site1, Site.class);
//      restTemplate.postForEntity(getRootUrl() + "/sites", site2, Site.class);
//      restTemplate.postForEntity(getRootUrl() + "/sites", site3, Site.class);

        User calledUser1 = restTemplate.getForObject(getRootUrl() + "/users/" + user1.getUsername(), User.class);
        System.out.println("Username: " + calledUser1.getUsername() + ", Password: " + calledUser1.getPassword());
        for (Site site : calledUser1.getSites()) {
            System.out.println("Sign up site: " + site.getSiteURL());
        }
        User calledUser2 = restTemplate.getForObject(getRootUrl() + "/users/" + user2.getUsername(), User.class);
        System.out.println("Username: " + calledUser2.getUsername() + ", Password: " + calledUser2.getPassword());
        for (Site site : calledUser2.getSites()) {
            System.out.println("Sign up site: " + site.getSiteURL());
        }
    }

这是错误代码。

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.t3q.userMange.model.User] and content type [application/json;charset=UTF-8]
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:121)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:995)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:978)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:737)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670)
    at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:445)
    at org.springframework.boot.test.web.client.TestRestTemplate.postForEntity(TestRestTemplate.java:506)
    at userTest.userTest.createDataset(userTest.java:171)
    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:567)
    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.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

当我使用上面测试代码(MediaType.ALL)的转换器时,出现了另一个错误。

java.lang.IllegalArgumentException: Content-Type cannot contain wildcard type '*'
    at org.springframework.util.Assert.isTrue(Assert.java:118)
    at org.springframework.http.HttpHeaders.setContentType(HttpHeaders.java:915)
    at org.springframework.http.converter.AbstractHttpMessageConverter.addDefaultHeaders(AbstractHttpMessageConverter.java:256)
    at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:87)
    at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:939)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:733)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670)
    at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:445)
    at org.springframework.boot.test.web.client.TestRestTemplate.postForEntity(TestRestTemplate.java:506)
    at userTest.userTest.createDataset(userTest.java:172)
    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:567)
    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.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

我该如何解决这个问题?

0 个答案:

没有答案