java - RESTful service in Spring configuration with jackson-datatype-jsr310 -


i trying configure java 8 & spring 4.3.1 app use restful services. work config below, until introduce contextresolver.

the reason contextresolver because need format java.time.localdatetime json.

i first tried annotations on model bean adding @jsonformat,

@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; 

and got following error:

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;

secondly, removed @jsonformat annotation , tried 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;     }   } 

and got following error:

[org.springframework.web.servlet.pagenotfound] (default task-4) no mapping found http request uri [/jbosswildfly/employee/list] in dispatcherservlet name 'rest'

please can advise, think spring configuration may not correct.

  • in first case, @jsonformat annotation, request hits restful service, gets nosuchmethoderror suggesting dependencies not correct.
  • the second case, contextresolver, request ** doesn't find dispatcherservelet** (and hence doesn't reach restful service). suggests spring config incorrect.

as said, if don't use @jsonformat annotation or contextresolver, can call restful service (but need format dates).

thank you

my config as follows:

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> 

rest controller

@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);     } } 

solved

by adding following class, works now:

package com.jobs.spring.configuration;  import java.text.simpledateformat; import java.util.list;  import org.springframework.context.annotation.configuration; import org.springframework.http.converter.httpmessageconverter; import org.springframework.http.converter.json.jackson2objectmapperbuilder; import org.springframework.http.converter.json.mappingjackson2httpmessageconverter; import org.springframework.http.converter.xml.mappingjackson2xmlhttpmessageconverter; import org.springframework.web.servlet.config.annotation.enablewebmvc; import org.springframework.web.servlet.config.annotation.webmvcconfigureradapter;  @configuration @enablewebmvc public class webconfiguration extends webmvcconfigureradapter {      @override     public void configuremessageconverters(list<httpmessageconverter<?>> converters) {         jackson2objectmapperbuilder builder = new jackson2objectmapperbuilder();         builder.indentoutput(true).dateformat(new simpledateformat("yyyy-mm-dd"));         converters.add(new mappingjackson2httpmessageconverter(builder.build()));         converters.add(new mappingjackson2xmlhttpmessageconverter(builder.createxmlmapper(true).build()));     } } 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -