先说解决方案,在pom中加入如下依赖,解决。
<!--json-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
说起来,这可真真是一个坑爹的问题,困扰了一天,烦了一天,我真以为我大半年没碰java,连新建个sprintboot的rest接口用完美的intellij idea这么高大上的工具都创建不出来了呢。
接口
看看错误吧.
两个连入门都算不上的接口
package com.marvin.demo.controller;
import com.marvin.demo.vo.DemoEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
/**
* 精诚所至,金石为开。
* 石の上にも三年;陽気の発する所金石亦透る。
*
* @Author mahaiqiang
* @Create 2020/2/1 11:06 下午
* @Description TODO
**/
@Slf4j
@RestController
public class DemoController {
@GetMapping("/demo1")
public DemoEntity demo1() {
log.info("第一个get接口");
DemoEntity de = new DemoEntity();
de.setName("李四");
return de;
}
@PostMapping("/demo2")
public DemoEntity demo2(@RequestBody DemoEntity entity) {
log.info("第一个post接口, 参数:{}", entity);
entity.setName("Hello, " + entity.getName());
return entity;
}
}
错误
GET是正常的,但是POST的这个接口,因为用了@RequestBody
注解,要转换json,所以就死活不行。
错误是这样的:
2020-02-03 22:55:34.382 WARN 40841 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported]
2020-02-03 22:55:34.405 WARN 40841 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.LinkedHashMap]
出这个错我先上网查,有个推荐找不到了说引入jackson
看日志我引入的是2.9.6不知道是不是还少引用了啥,反正没成。
让别的同事试了下,人家说啥也不用干,导入进去就能运行,日了狗了,咋回事,肯定就是环境问题了呗。
于是就jdk、maven、intellij idea,重新配置,甚至重装,都搞一遍也没办法了。
第二天继续按照错误提示找,找到个原来就翻出来过的链接,加了依赖后莫名其妙好了,而且删除了依赖立马就能复现问题了。
正常
预期是这样的
com.marvin.demo.DemoApplication
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.4.RELEASE)
2020-02-03 22:51:16.565 INFO 40753 --- [ main] com.marvin.demo.DemoApplication : Starting DemoApplication on 192.168.0.105 with PID 40753 (/Users/mahaiqiang/git/mygitrepo/secret-demo/target/classes started by mahaiqiang in /Users/mahaiqiang/git/mygitrepo/secret-demo)
2020-02-03 22:51:16.571 INFO 40753 --- [ main] com.marvin.demo.DemoApplication : No active profile set, falling back to default profiles: default
2020-02-03 22:51:17.751 INFO 40753 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-02-03 22:51:17.763 INFO 40753 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-02-03 22:51:17.764 INFO 40753 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.30]
2020-02-03 22:51:17.838 INFO 40753 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-02-03 22:51:17.838 INFO 40753 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1205 ms
2020-02-03 22:51:18.043 INFO 40753 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-02-03 22:51:18.230 INFO 40753 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-02-03 22:51:18.234 INFO 40753 --- [ main] com.marvin.demo.DemoApplication : Started DemoApplication in 2.039 seconds (JVM running for 2.681)
2020-02-03 22:51:29.934 INFO 40753 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-02-03 22:51:29.934 INFO 40753 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-02-03 22:51:29.942 INFO 40753 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 8 ms
2020-02-03 22:51:30.045 INFO 40753 --- [nio-8080-exec-1] c.marvin.demo.controller.DemoController : 第一个post接口, 参数:DemoEntity(name=a)
2020-02-03 22:51:50.866 INFO 40753 --- [nio-8080-exec-3] c.marvin.demo.controller.DemoController : 第一个post接口, 参数:DemoEntity(name=marvin)
这次是百度救了我,我一般用的必应搜索引擎,没想到按照第二个错误百度出来的第一条直接就是解决方案。
但是按照方案说第一条,我的pom文件里已经有spring-boot-starter-web
,
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
但是还是逼着我加了jackson才好使了。
先记录一笔,再慢慢想原因吧,希望日后不会再碰到这么恶心的问题。
感谢
SpringBoot2.X 遭遇 No converter found for return value of type: class java.util.LinkedHashMap
网友评论