java - 在自定义 Controller 中解析实体 URI (Spring HATEOAS)

标签 java spring rest spring-data-rest spring-hateoas

我有一个基于 spring-data-rest 的项目,它也有一些自定义端点。

为了发送 POST 数据,我使用 json 之类的

{
 "action": "REMOVE",
 "customer": "http://localhost:8080/api/rest/customers/7"
}

这对于 spring-data-rest 来说很好,但不适用于自定义 Controller 。

例如:

public class Action {
    public ActionType action;
    public Customer customer;
}

@RestController
public class ActionController(){
  @Autowired
  private ActionService actionService;

  @RestController
  public class ActionController {
  @Autowired
  private ActionService actionService;

  @RequestMapping(value = "/customer/action", method = RequestMethod.POST)
  public ResponseEntity<ActionResult> doAction(@RequestBody Action action){
    ActionType actionType = action.action;
    Customer customer = action.customer;//<------There is a problem
    ActionResult result = actionService.doCustomerAction(actionType, customer);
    return ResponseEntity.ok(result);
  }
}

当我打电话时

curl -v -X POST -H "Content-Type: application/json" -d '{"action": "REMOVE","customer": "http://localhost:8080/api/rest/customers/7"}' http://localhost:8080/customer/action

我有答案

{
"timestamp" : "2016-05-12T11:55:41.237+0000",
"status" : 400,
"error" : "Bad Request",
"exception" : "org.springframework.http.converter.HttpMessageNotReadableException",
"message" : "Could not read document: Can not instantiate value of type [simple type, class model.user.Customer] from String value ('http://localhost:8080/api/rest/customers/7'); no single-String constructor/factory method\n at [Source: java.io.PushbackInputStream@73af10c6; line: 1, column: 33] (through reference chain: api.controller.Action[\"customer\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class logic.model.user.Customer] from String value ('http://localhost:8080/api/rest/customers/7'); no single-String constructor/factory method\n at [Source: java.io.PushbackInputStream@73af10c6; line: 1, column: 33] (through reference chain: api.controller.Action[\"customer\"])",
"path" : "/customer/action"
* Closing connection 0
}

因为 spring 无法将 URI 转换为 Customer 实体。

有没有办法使用 spring-data-rest 机制通过 URI 解析实体?

我只有一个想法 - 使用带有解析 URI 的自定义 JsonDeserializer 来提取 entityId 并向存储库发出请求。但是,如果我有像“http://localhost:8080/api/rest/customers/8/product”这样的 URI,这种策略对我没有帮助,在这种情况下,我没有 product.Id 值。

最佳答案

我也有同样的问题很长时间了,并通过以下方式解决了它。 @Florian 走在正确的轨道上,感谢他的建议,我找到了一种自动进行转换的方法。需要几件:

  1. 实现从 URI 到实体的转换的转换服务(利用框架提供的 UriToEntityConverter)
  2. 一个反序列化器,用于检测何时适合调用转换器(我们不想弄乱默认的 SDR 行为)
  3. 将所有内容推送到 SDR 的自定义 Jackson 模块

对于第 1 点,实现可以缩小到以下内容

import org.springframework.context.ApplicationContext;
import org.springframework.data.mapping.context.PersistentEntities;
import org.springframework.data.repository.support.DomainClassConverter;
import org.springframework.data.rest.core.UriToEntityConverter;
import org.springframework.format.support.DefaultFormattingConversionService;

public class UriToEntityConversionService extends DefaultFormattingConversionService {

   private UriToEntityConverter converter;

   public UriToEntityConversionService(ApplicationContext applicationContext, PersistentEntities entities) {
      new DomainClassConverter<>(this).setApplicationContext(applicationContext);

       converter = new UriToEntityConverter(entities, this);

       addConverter(converter);
   }

   public UriToEntityConverter getConverter() {
      return converter;
   }
}

对于第 2 点,这是我的解决方案

import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.BeanDeserializerBuilder;
import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier;
import com.fasterxml.jackson.databind.deser.ValueInstantiator;
import com.fasterxml.jackson.databind.deser.std.StdValueInstantiator;
import your.domain.RootEntity; // <-- replace this with the import of the root class (or marker interface) of your domain
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.context.PersistentEntities;
import org.springframework.data.rest.core.UriToEntityConverter;
import org.springframework.util.Assert;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Optional;


public class RootEntityFromUriDeserializer extends BeanDeserializerModifier {

   private final UriToEntityConverter converter;
   private final PersistentEntities repositories;

   public RootEntityFromUriDeserializer(PersistentEntities repositories, UriToEntityConverter converter) {

       Assert.notNull(repositories, "Repositories must not be null!");
       Assert.notNull(converter, "UriToEntityConverter must not be null!");

       this.repositories = repositories;
       this.converter = converter;
   }

   @Override
   public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, BeanDescription beanDesc, BeanDeserializerBuilder builder) {

       PersistentEntity<?, ?> entity = repositories.getPersistentEntity(beanDesc.getBeanClass());

       boolean deserializingARootEntity = entity != null && RootEntity.class.isAssignableFrom(entity.getType());

       if (deserializingARootEntity) {
           replaceValueInstantiator(builder, entity);
       }

       return builder;
   }

   private void replaceValueInstantiator(BeanDeserializerBuilder builder, PersistentEntity<?, ?> entity) {
      ValueInstantiator currentValueInstantiator = builder.getValueInstantiator();

       if (currentValueInstantiator instanceof StdValueInstantiator) {

          EntityFromUriInstantiator entityFromUriInstantiator =
                new EntityFromUriInstantiator((StdValueInstantiator) currentValueInstantiator, entity.getType(), converter);

          builder.setValueInstantiator(entityFromUriInstantiator);
       }
   }

   private class EntityFromUriInstantiator extends StdValueInstantiator {
      private final Class entityType;
      private final UriToEntityConverter converter;

      private EntityFromUriInstantiator(StdValueInstantiator src, Class entityType, UriToEntityConverter converter) {
         super(src);
         this.entityType = entityType;
         this.converter = converter;
      }

      @Override
      public Object createFromString(DeserializationContext ctxt, String value) throws IOException {
         URI uri;
         try {
            uri = new URI(value);
         } catch (URISyntaxException e) {
            return super.createFromString(ctxt, value);
         }

         return converter.convert(uri, TypeDescriptor.valueOf(URI.class), TypeDescriptor.valueOf(entityType));
      }
   }
}

那么对于第 3 点,在自定义 RepositoryRestConfigurerAdapter 中,

public class MyRepositoryRestConfigurer extends RepositoryRestConfigurerAdapter {
   @Override
   public void configureJacksonObjectMapper(ObjectMapper objectMapper) {
      objectMapper.registerModule(new SimpleModule("URIDeserializationModule"){

         @Override
         public void setupModule(SetupContext context) {
            UriToEntityConverter converter = conversionService.getConverter();

            RootEntityFromUriDeserializer rootEntityFromUriDeserializer = new RootEntityFromUriDeserializer(persistentEntities, converter);

            context.addBeanDeserializerModifier(rootEntityFromUriDeserializer);
         }
      });
   }
}

这对我来说很顺利,并且不会干扰框架的任何转换(我们有许多自定义端点)。在第 2 点中,目的是仅在以下情况下启用从 URI 的实例化:

  1. 被反序列化的实体是根实体(因此没有属性)
  2. 提供的字符串是一个实际的 URI(否则它只是回退到默认行为)

关于java - 在自定义 Controller 中解析实体 URI (Spring HATEOAS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37186417/

相关文章:

java - Spring 休息 : Removing empty objects from response in JSON format

java - 堆混淆和堆数组

java - Spring,JXLS : XLS transformer not loaded,,即使依赖存在。

spring - 在 Spring Boot 应用程序中提取 docker secret 而不是使用属性文件

java - 使用bean引用从spring xml配置迁移到@Configuration(servlet 3.0)会导致BeanNotOfRequiredTypeException

json - 使用 angularjs 进行 django-rest 分页

javascript - 如何按照发送请求的顺序将获取响应数据分配给数组

java - Eclipse 服务器 View 未显示添加的 Tomcat 运行时环境

java - 对象化查询 : setting limit above 300 does not work

spring - 如何在 Spring Boot 2.1.0 登录时禁用 Set-Cookie header 上的 HttpOnly 标志