Spring REST - 从Entity构建链接

时间:2014-09-08 09:43:58

标签: java spring spring-mvc jpa

我使用Spring + JPA并将实体映射为数据库视图,我获得了所有字段。

现在我想为这个实体添加一个字段,并根据另一个字段返回一个链接图像(所有图像都存储在资源/图像中)

@Entity
@Table(name = "Release")
public class Release implements Serializable
{
  private String user;
  private String userImageLink; // This is what i need to implement

  @XmlElement
  @JsonProperty
  public String getUser(){
    return user;
  }
  public void setUser(String user) {
    this.user = user;
  }

  public String getUserImageLink(){
    String imageUrl = /*i need to access to resource/images and create the link
                       If i run on localhost should return: http://127.0.0.1:8080/resources/images..
                       In production coudl return: http://myServer.com/resources/images..     
                      */;
    return imageUrl;
  }
}
}

问题是:我如何创建并返回此网址?

更多信息

如果表格有3个条目:User1,User2,User3

在userImageLink中我应该返回:

http://myserver.com/resources/images/User1.jpg http://myserver.com/resources/images/User2.jpg

如果图片在资源/图片中存在或不存在,那么重要,我只需要在访问从我的"发布&中获取数据的服务时将网址组合为附加参数#34; @Entity

3 个答案:

答案 0 :(得分:2)

您可以使用应用ServletContext生成图片的网址。 这个ServletContext不能作为POJO访问到您的JPA实体中,但是应该作为一个bean注入Spring容器,否则最终会得到一个null引用:

  • 在您的实体中将静态实例变量自动装配为ServletContext
@Entity
@Table(name = "Release")
public class Release implements Serializable
{
  @Transient
  private static ServletContext servletContext;

  @PostConstruct
  public void init() {
    log.info("Initializing ServletContext as [" +
                Release.servletContext + "]"); //Forces injection after bean construction.
  }

  @Autowired
  public void setServletContext(ServletContext servletContext) {
    Release.servletContext = servletContext; //Note the use of class name because that fiels hould be static thus shared by all instances.
  }
  //...
}
  • 使用注入的ServletContext检索您的webapp上下文路径并相应地构建您的图片网址:
@Entity
@Table(name = "Release")
public class Release implements Serializable
{
  //...
  public String getUserImageLink(){
    StringBuilder imageUrlBuilder = new StringBuilder(Release.servletContext.getContextPath())
        .append("/resources/images")
        .append(this.user);
    return imageUrlBuilder.toString();
  }
}

请记住添加基于annoation的bean scannig以识别您的servlet上下文bean,并使用Release标记您的@Component类,以便对其进行扫描:

<beans>
  <!-- ... -->
  <context:component-scan base-package="package.of.Release" />
<beans>

感谢@Ravi Thapliyal sharing the tip

答案 1 :(得分:1)

1)将 userImageLink 标记为@Transient,因为JPA将使用该类的所有属性,除非您使用@Transient专门标记它们,如下所示:

@Transient
private String userImageLink;

2)一旦 发布 实体类填充了DB值,然后假设httpservletrequest对象可用:

StringBuilder baseURL = new StringBuilder();
baseURL.append(request.getRequestURL().toString().replace(request.getRequestURI(), ""));

baseURL.append("/resources/images/").append(release.getUser());

release.setUserImageLink(baseURL);

答案 2 :(得分:1)

https://stackoverflow.com/a/24122238/934719为这个问题提供了一个非常简单的解决方案,无需自动装配等工作:

public static String getContextParameter(String param){
    try {
        InitialContext initialContext = new InitialContext();
        Context environmentContext = (Context) initialContext.lookup("java:/comp/env");
        return (String) environmentContext.lookup(param);
    } catch (NamingException e) {
        e.printStackTrace();
    }
    return null;
}