gpt4 book ai didi

java - Spring-boot + Hibernate + JPA 与 transient 字段

转载 作者:行者123 更新时间:2023-12-01 11:00:18 27 4
gpt4 key购买 nike

这是我的第一个问题,但我已经寻找解决方案两天了,但没有成功。在我的项目中,我有一个具有 transient 属性的 User 实体:

@Transient
@JsonProperty
private List<String> files;

我没有 setter,getter 是:

public List<String> getFiles() {
/* Call one static method */
}

在调试中使用 NetBeans 执行应用程序,工作正常,并且从 javascript,我可以使用 user.fotos 获取 getFiles 结果。但是,当我生成 .jar 文件,并使用命令 java -jar app.jar 执行应用程序时,通过调用一个必须返回一个 User 对象的 Rest 函数,我得到这个异常(exception):

2015-10-28 14:39:35.963  WARN 27836 --- [nio-7777-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: (was java.lang.NullPointerException) (through reference chain: com.pfc.soriano.wsdbmodel.entity.User["files"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.pfc.soriano.wsdbmodel.entity.User["files"])
2015-10-28 14:39:35.963 WARN 27836 --- [nio-7777-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Handler execution resulted in exception: Could not write content: (was java.lang.NullPointerException) (through reference chain: com.pfc.soriano.wsdbmodel.entity.User["files"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.pfc.soriano.wsdbmodel.entity.User["files"])

我的问题是:Netbeans 与命令行 java -jar 有何不同,这使得它可以正常工作?

最佳答案

试图找到更多文档,我发现了这个:JPA Transient Annotation and JSON

感谢 Damien,我的项目终于可以正常运行了:主应用程序.java:

@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter {

/* Here we register the Hibernate4Module into an ObjectMapper, then set this custom-configured ObjectMapper
* to the MessageConverter and return it to be added to the HttpMessageConverters of our application*/
public MappingJackson2HttpMessageConverter jacksonMessageConverter() {
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();

ObjectMapper mapper = new ObjectMapper();
Hibernate4Module hm = new Hibernate4Module();
hm.disable(Hibernate4Module.Feature.USE_TRANSIENT_ANNOTATION);
mapper.registerModule(hm);

messageConverter.setObjectMapper(mapper);
return messageConverter;
}

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(jacksonMessageConverter());
super.configureMessageConverters(converters);
}

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

用户实体类:

@Entity
@Table(name = "user")
public class User implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "ID")
private Long id;
@Transient
Collection<String> files;

public Long getId() {
files = Utils.getImages("" + id, "src/main/webapp/user/");
return id;
}

public void setId(Long id) {
this.id = id;
}

public Collection<String> getFiles() {
return files;
}

public void setFiles(Collection<String> files) {
this.files = files;
}
}

UserDAO 接口(interface):

@RepositoryRestResource(collectionResourceRel = "users", itemResourceRel = "users")
public interface UserDAO extends JpaRepository<User, Long> {

}

用户 Controller :

@Controller
@RequestMapping(value = "user")
public class UserController {

@Autowired
UserDAO userDAO;

@RequestMapping(value = "findById", method = RequestMethod.POST)
@ResponseBody
public User findById(@Param("id") Long id) {
return userDAO.findOne(id);
}
}

Javascript函数:

function loadUser(id) {
$.ajax({
type: "POST",
url: serviceBaseUrl + "user/findById",
data: {id: id},
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
success: function (data, textStatus, jqXHR) {
if(data) {
alert(data.files);
/* DO SOMETHING ELSE */
}
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR.responseJSON.message);
}
});
}

结果是 JavaScript 向我显示一条警报,其中包含服务器上存在的文件名。

关于java - Spring-boot + Hibernate + JPA 与 transient 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33393398/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com