Recently, I made such a 406 mistake when I followed the Taotao mall project.
This is the code in the Controller
@RequestMapping("/items/{itemId}") @ResponseBody public TbItem getItemById(@PathVariable long itemId) { TbItem item=itemService.findItemById(itemId); return item; }
This is the json conversion package dependency in tatao Manager Web:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.7.3</version> </dependency>
This is the po class TbItem
public class TbItem { private Long id; private String title; private String sellPoint; private Long price; private Integer num; private String barcode; private String image; private Long cid; private Byte status; private Date created; private Date updated; //Omit getter and setter methods
Note that this class now has a Date type property created, which is suspected to be the problem.
1. Change dependence
<!-- jackson Dependent package --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.7.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.7.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.4</version> </dependency>
One bag becomes three.
2. Change the spring MVC configuration file
<!-- MappingJackson2HttpMessageConverter Handle responseBody Date type inside xsi:schemaLocation Import version must be greater than or equal to 3.1 --> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> <property name="objectMapper"> <bean class="com.fasterxml.jackson.databind.ObjectMapper"> <property name="dateFormat"> <bean class="java.text.SimpleDateFormat"> <constructor-arg type="java.lang.String" value="yyyyMMddHHmmss" /> </bean> </property> </bean> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
My configuration file is called springmvc.xml. This section is to set the date format of json response globally. There is another way. You can see the following reference.
OK, run again, come out.
Although the problem has been solved, I'm not sure what causes it. I'll learn more later.
Reference resources:
[1],Jackson details