在升级到新发布2.2.0.RELEASE的 Spring Boot 版本后,我的一些测试失败了。看来,MediaType.APPLICATION_JSON_UTF8已弃用并且不再作为未明确指定内容类型的控制器方法的默认内容类型返回。
测试代码如
String content = mockMvc.perform(get("/some-api")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andReturn()
.getResponse()
.getContentAsString();
Run Code Online (Sandbox Code Playgroud)
突然不再工作,因为内容类型不匹配,如下所示
java.lang.AssertionError: Content type
Expected :application/json;charset=UTF-8
Actual :application/json
Run Code Online (Sandbox Code Playgroud)
将代码更改为 .andExpect(content().contentType(MediaType.APPLICATION_JSON))暂时解决问题。
但是现在当与content预期的序列化对象进行比较时,如果对象中有任何特殊字符,仍然存在不匹配。似乎该.getContentAsString()方法默认不使用 UTF-8 字符编码(不再使用)。
java.lang.AssertionError: Response content expected:<[{"description":"Er hörte leise Schritte hinter sich."}]> but was:<[{"description":"Er hörte leise Schritte hinter sich."}]>
Expected :[{"description":"Er hörte leise Schritte hinter sich."}]
Actual :[{"description":"Er hörte leise Schritte hinter sich."}]
Run Code Online (Sandbox Code Playgroud)
我怎样才能进入contentUTF-8 编码?
我目前正在开发一个带有 JPA/Hibernate 和 Angular 2 前端的多用户 Spring Boot 应用程序。我设想了两种基本类型的用户:供应商和客户。
在开始编码之前,我制作了一个 UML 类图,其中包含该类User和两个子类Customer以及Vendor. 子类的原因是它们具有不同的属性:例如,供应商应该能够编写关于他自己的一般描述,而客户应该能够保存收藏夹。
下图是我的类图的摘录:
由于这是我的第一个具有登录/注册功能的应用程序,我开始阅读有关如何使用 Spring Security 实现登录和注册功能的教程。随着时间的推移,我不得不意识到基本上所有的样本都只包含一个User类,其权限取决于他的Roles,这通常是实现上述功能的推荐/需要的类。
在我看来,User该类并不意味着有子类,但教程和示例自然主要涵盖基本设置,而不是具有完全不同用户的复杂业务模型的表示。
我的问题如下:创建User我想用于授权自定义UserDetailsService类的类的子类是否可行和可取?如果没有,您是否建议将Customer和Vendor类中的属性合并到User类中,从而确定User的角色是否应该能够拥有某个属性,否则它将为空?
非常感谢!
我想知道是否可以为 Spring 的CrudRepositorysave 方法定义自定义返回类型,就像下面的 find 查询方法示例一样:
<T> Optional<T> findById(Long id, Class<T> type);
Run Code Online (Sandbox Code Playgroud)
在文档中,我只找到了带有查询方法的动态投影的示例。
我试过
<T> T save(Foo entity, Class<T> type);
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
java.lang.IllegalArgumentException: Failed to create query for method public abstract java.lang.Object com.xyz.myproject.persistence.dao.FooDAO.save(com.xyz.myproject.persistence.model.Foo,java.lang.Class)! No property save found for type Foo!
Run Code Online (Sandbox Code Playgroud)
有什么想法如何让它发挥作用吗?