2016-07-12 6 views
3

Java 8 & Spring 4.3.1アプリケーションでRESTfulサービスを使用するように構成しようとしています。 ContextResolverを紹介するまで、以下の設定で完全に動作するようになりました。jsonson-datatype-jsr310を使用したSpring構成のRESTfulサービス

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] (default task-4) No mapping found for HTTP request with URI [/jbosswildfly/employee/list] in DispatcherServlet with name 'rest'

を、私は私の春の構成が正しくないかもしれないと思います。最初のケースで

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

残り:

次のようとして私の設定ありがとう-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); 
    } 
} 
+0

あなたは安らか何フレームワークを使用していますか?春のRestControllerまたはジャージ? RestControllerまたはResourceクラスのコードを投稿することはできますか? – Wilson

+0

こんにちはウィルソン、助けてくれてありがとう。私はjackson-datatype-jsr310で問題が発生していたので、代わりに@XmlJavaTypeAdapterを試しています。あなたが気にしない場合は、http://stackoverflow.com/questions/38335917/xmljavatypeadapter-the-xmladapter-never-gets-invokedをご覧ください。 – Richard

答えて

2

は以下のクラスを追加することにより

を解決し、それが動作するようになりました:

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())); 
    } 
} 
関連する問題