Feign and HAL / Resources

I have a server that provides resources via spring-data-rest, and this uses, as far as I understand, HAL or HATEOAS. But when I try to use it in combination with Feign, I seem to be unable to register the selected Jackson2HalModule.

Is there something I need to do to connect the Feign β€œclient” to the new converter? Does it use a different ObjectMapper than the one I got here?

the code:

@Inject
public void configureObjectMapper(ObjectMapper mapper, RestTemplate template) {
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.registerModule(new Jackson2HalModule());

    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/hal+json"));
    converter.setObjectMapper(mapper);

    template.getMessageConverters().add(converter);
}

The response from the server:

{
  "_links" : {
    "self" : {
      "href" : "http://localhost:13372/user{?page,size,sort}",
      "templated" : true
    },
    "search" : {
      "href" : "http://localhost:13372/user/search"
    }
  },
  "_embedded" : {
    "user" : [ {
      "id" : "5567613e5da543dba4201950",
      "version" : 0,
      "created" : "2015-05-28T18:41:02.685Z",
      "createdBy" : "system test",
      "edited" : "2015-05-28T18:41:02.713Z",
      "editedBy" : "system test",
      "username" : "devuser",
      "email" : "dev@test.com",
      "roles" : [ "USER" ],
      "_links" : {
        "self" : {
          "href" : "http://localhost:13372/user/5567613e5da543dba4201950"
        }
      }
    } ]
  },
  "page" : {
    "size" : 20,
    "totalElements" : 1,
    "totalPages" : 1,
    "number" : 0
  }
}

An exception:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
 at [Source: java.io.PushbackInputStream@7b6c6e70; line: 1, column: 1]
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:762)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:758)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:275)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:216)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:206)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3066)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2221)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:205)
+4
source share
5 answers

This worked for me:

@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
@SpringBootApplication
@EnableFeignClients
public class Application {
....
}

Check out @EnableHypermediaSupport

@FeignClient(url = "http://localhost:8080")
public interface PersonClient {
    @RequestMapping(method = RequestMethod.GET, value = "/persons")
    Resources<Person> getPersons();

    @RequestMapping(method = RequestMethod.GET, value = "/persons/{id}")
    Resource<Person> getPerson(@PathVariable("id") long id);
}

I created a fully working example here: https://github.com/jtdev/spring-feign-data-rest-example

, wench maven pom spring -boot spring -cloud ( ) json.

+5

. - , API REST . .

( ):

mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

: , FeignClient :

@Service
@FeignClient(UsersConstants.USER_SERVICE_NAME)
public interface UsersServices {

    @RequestMapping(method = RequestMethod.GET, value = "/user")
    List<User> getUsers();
}

, , :

@Service
@FeignClient(UsersConstants.USER_SERVICE_NAME)
public interface UsersServices {

    @RequestMapping(method = RequestMethod.GET, value = "/user")
    List<PagedResources<User>> getUsers();
}

PagedResource HATEOAS:

<dependency>
    <groupId>org.springframework.hateoas</groupId>
    <artifactId>spring-hateoas</artifactId>
</dependency>

, , , ..

+4

feignDecoder bean . spring-hateoas , - :

@Bean
public Decoder feignDecoder() {
    ObjectMapper mapper = new ObjectMapper()
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
            .registerModule(new Jackson2HalModule());

    return new ResponseEntityDecoder(new JacksonDecoder(mapper));
}

HAL PagedResource.

0

, ( @Devabc, , , ):

PagedResources .

@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL) .

My Feign :

@FeignClient("user-microservice")
public interface UserClient {
  @RequestMapping(method = RequestMethod.GET, value = "/user")
  PagedResources<Resource<User>> findAll();
}

. ( User) , , , , , .

, Feign

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-feign</artifactId>
  <version>1.1.5.RELEASE</version>
</dependency>
0

Greg Turnquist

C) Resources<Resource<Question>>. , .

0

All Articles