从为具有复合ID的实体定制HATEOAS链接生成中学习后,我发现了一个更为通用的解决方案。
首先,创建一个SpringUtil以从Spring获取bean。
import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component;@Componentpublic class SpringUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { if(SpringUtil.applicationContext == null) { SpringUtil.applicationContext = applicationContext; } } public static ApplicationContext getApplicationContext() { return applicationContext; } public static Object getBean(String name){ return getApplicationContext().getBean(name); } public static <T> T getBean(Class<T> clazz){ return getApplicationContext().getBean(clazz); } public static <T> T getBean(String name,Class<T> clazz){ return getApplicationContext().getBean(name, clazz); }}
然后,实现BackendIdConverter。
import com.alibaba.fastjson.JSON;import com.example.SpringUtil;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.data.rest.webmvc.spi.BackendIdConverter;import org.springframework.stereotype.Component;import javax.persistence.EmbeddedId;import javax.persistence.Id;import java.io.Serializable;import java.io.UnsupportedEncodingException;import java.lang.reflect.Method;import java.net.URLDeprer;import java.net.URLEnprer;@Componentpublic class CustomBackendIdConverter implements BackendIdConverter { @Override public boolean supports(Class<?> delimiter) { return true; } @Override public Serializable fromRequestId(String id, Class<?> entityType) { if (id == null) { return null; } //first depre url string if (!id.contains(" ") && id.toUpperCase().contains("%7B")) { try { id = URLDeprer.depre(id, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } //deserialize json string to ID object Object idObject = null; for (Method method : entityType.getDeclaredMethods()) { if (method.isAnnotationPresent(Id.class) || method.isAnnotationPresent(EmbeddedId.class)) { idObject = JSON.parseObject(id, method.getGenericReturnType()); break; } } //get dao class from spring Object daoClass = null; try { daoClass = SpringUtil.getBean(Class.forName("com.example.db.dao." + entityType.getSimpleName() + "DAO")); } catch (ClassNotFoundException e) { e.printStackTrace(); } //get the entity with given primary key JpaRepository simpleJpaRepository = (JpaRepository) daoClass; Object entity = simpleJpaRepository.findOne((Serializable) idObject); return (Serializable) entity; } @Override public String toRequestId(Serializable id, Class<?> entityType) { if (id == null) { return null; } String jsonString = JSON.toJSonString(id); String enpredString = ""; try { enpredString = URLEnprer.enpre(jsonString, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return enpredString; }}
之后。你可以做你想做的。
下面有一个示例。
- 如果实体具有单个属性pk,则可以正常使用localhost:8080 / demo / 1。根据我的代码,假设pk具有注释“ @Id”。
- 如果实体由pk组成,假设pk是demoId类型,并且具有注释“ @EmbeddedId”,则可以使用localhost:8080 / demo / {demoId json}进行获取/放置/删除。并且您的自我链接将是相同的。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)