【发布时间】:2016-11-14 18:23:05
【问题描述】:
我正在尝试配置 Java 8 和 Spring 4.3.1 应用程序以使用 RESTful 服务。在我引入 ContextResolver 之前,我让它与下面的配置完美配合。
使用 ContextResolver 的原因是因为我需要将 java.time.LocalDateTime 格式化为 JSON。
我首先通过添加 @JsonFormat 在我的模型 bean 上尝试使用 注释,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DATE_FORMAT)
@DateTimeFormat(pattern=DATE_FORMAT)
@Column(name = "JOINING_DATE", nullable = false)
@Type(type="org.hibernate.type.LocalDateTimeType")
private LocalDateTime joiningDate;
并得到以下错误:
java.lang.NoSuchMethodError: com.fasterxml.jackson.datatype.jsr310.ser.JSR310FormattedSerializerBase.findFormatOverrides(Lcom/fasterxml/jackson/databind/SerializerProvider;Lcom/fasterxml/jackson/databind/BeanProperty;Ljava/lang/Class;)Lcom/fasterxml/jackson/annotation /JsonFormat$Value;
其次,我删除了@JsonFormat注解并尝试使用ContextResolver,
ObjectMapperContextResolver.java
package com.jobs.spring.configuration;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
@Provider
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {
private final ObjectMapper MAPPER;
public ObjectMapperContextResolver() {
MAPPER = new ObjectMapper();
MAPPER.registerModule(new JavaTimeModule());
MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
}
@Override
public ObjectMapper getContext(Class<?> type) {
return MAPPER;
}
}
并得到以下错误:
[org.springframework.web.servlet.PageNotFound](默认任务4)否 为带有 URI [/jbosswildfly/employee/list] 的 HTTP 请求找到映射 在名为“rest”的 DispatcherServlet 中
请有人建议,我认为我的 Spring 配置可能不正确。
- 在第一种情况下,使用 @JsonFormat 注释,请求 点击 RESTful 服务,但收到 NoSuchMethodError 提示 我的依赖项不正确。
- 第二种情况,使用 ContextResolver,请求**不会 找到 DispatcherServelet** (因此没有达到 RESTful 服务)。这表明我的 Spring 配置不正确。
如我所说,如果我不使用@JsonFormat注解或ContextResolver,我可以成功调用RESTful服务(但我需要格式化日期)。
谢谢
我的配置如下:
pom.xml
.
.
.
<jackson.version>2.8.0</jackson.version>
.
.
.
<!-- JSON -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
metadata-complete="false">
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
rest-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.jobs.spring" />
<mvc:annotation-driven />
</beans>
休息控制器
@CrossOrigin(origins = {"*"})
@RestController
@RequestMapping(EmployeeRESTService.BASE_URI)
public class EmployeeRESTService {
public static final String BASE_URI = "/employee";
@Autowired
private EmployeeService employeeService;
@RequestMapping(value = "/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List<Employee> findAllEmployees() {
return employeeService.findAll();
}
@RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String saveEmployee(@RequestBody Employee employee){
Long id = employeeService.save(employee);
return Long.toString(id);
}
}
【问题讨论】:
-
你用什么restful框架? Spring RestController 还是 Jersey?你也可以发布 RestController 或 Resource 类的代码吗?
-
您好,威尔逊,感谢您的帮助。如您所见,我在使用 jackson-datatype-jsr310 时遇到问题,因此一直在尝试使用 @XmlJavaTypeAdapter。如果您不介意,请看一下stackoverflow.com/questions/38335917/…。