我想在我的实体中使用一个值对象作为数据库 ID。我也想要干净的 JSON。假设我有这个实体:
private static class TestEntity {
private TestEntityId id;
private String mutableProperty;
public TestEntity(TestEntityId id) {
this.id = id;
}
public TestEntityId getId() {
return id;
}
public String getMutableProperty() {
return mutableProperty;
}
public void setMutableProperty(String mutableProperty) {
this.mutableProperty = mutableProperty;
}
}
Run Code Online (Sandbox Code Playgroud)
其中有这个 id 类:
private static class TestEntityId extends ImmutableEntityId<Long> {
public TestEntityId(Long id) {
super(id);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我使用杰克逊的默认设置,我会得到这个:
{"id":{"id":1},"mutableProperty":"blabla"}
Run Code Online (Sandbox Code Playgroud)
但我想得到这个:
{"id":1,"mutableProperty":"blabla"}
Run Code Online (Sandbox Code Playgroud)
通过添加自定义序列化程序,我设法使序列化正常:
addSerializer(ImmutableEntityId.class, new JsonSerializer<ImmutableEntityId>() {
@Override
public void serialize(ImmutableEntityId immutableEntityId, JsonGenerator jsonGenerator, SerializerProvider …Run Code Online (Sandbox Code Playgroud) 我知道我可以使用以下方法将 Spring MVC 表单 bean 绑定到 LocalDateTime:
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private LocalDateTime startTime;
Run Code Online (Sandbox Code Playgroud)
这会将 String like2016-01-11T15:05:05.123Z转换为LocalDateTime对象。
我在这里找到了一些文档:http : //blog.codeleak.pl/2014/06/spring-4-datetimeformat-with-java-8.html
我找不到如何对java.time.Duration. 我该怎么做呢?
我需要在数据库中插入一些默认数据。我正在使用 Spring Boot 与 Flyway 集成。为了进行测试,我使用 H2。对于生产,将使用 MySQL。
我制作了单独的 Flyway 迁移脚本,因此我可以使用数据库特定的内容作为默认数据(创建表是在通用脚本中完成的)。
对于 MySQL,我有这样的东西:
INSERT INTO survey_definition (id, name, period)
VALUES (0x2D1EBC5B7D2741979CF0E84451C5BBB1, 'disease-activity', 'P1M');
Run Code Online (Sandbox Code Playgroud)
我怎样才能对 H2 做同样的事情?
我只找到了该RANDOM_UUID()函数,它可以工作,但我需要使用已知的 UUID,因为我在进一步的语句中将其用作外键。
我正在使用带有 JavaFX 的 Spring Boot 进行测试(基于一些解释这一点的优秀 YouTube 视频)。
为了使它与TestFX 一起工作,我需要创建这样的上下文:
@Override
public void init() throws Exception {
SpringApplicationBuilder builder = new SpringApplicationBuilder(MyJavaFXApplication.class);
builder.headless(false); // Needed for TestFX
context = builder.run(getParameters().getRaw().stream().toArray(String[]::new));
FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
loader.setControllerFactory(context::getBean);
rootNode = loader.load();
}
Run Code Online (Sandbox Code Playgroud)
我现在想测试这个 JavaFX 应用程序,为此我使用:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class MyJavaFXApplicationUITest extends TestFXBase {
@MockBean
private MachineService machineService;
@Test
public void test() throws InterruptedException {
WaitForAsyncUtils.waitForFxEvents();
verifyThat("#statusText", (Text text ) -> text.getText().equals("Machine stopped"));
clickOn("#startMachineButton");
verifyThat("#startMachineButton", …Run Code Online (Sandbox Code Playgroud) 我正在寻找一个模拟StopWatch的java类(如Spring的StopWatch或Commons的StopWatch),但它会给出最小,最大和平均时间.也优选在最后n次运行中的平均值.
有这样的事情还是我自己建造它?
问候,
维姆
我正在实现一个带有 REST 后端的 AngularJS 应用程序(使用 Spring Boot)。
我目前可以下载这样的文件:
<td><a href="/api/datasheets/{{datasheet.id}}/documents/{{document.id}}/download" download>Download</a></td>
Run Code Online (Sandbox Code Playgroud)
现在,我正在为我的应用程序添加安全性(使用 Spring Security),现在这不再有效。AJAX 调用的身份验证通过x-auth-token为每个请求添加HTTP 标头来实现。
但是一个简单href的当然x-auth-token在标题中没有。我尝试$http.get()在 an 上使用ng-click,但这不行。
有没有简单的替代方案?
我正在尝试保护Spring Boot执行器端点.我在/apiREST界面上有安全性,但尝试在内置端点上添加安全性似乎不起作用.
我在我的端点中设置了端点分组application.properties:
management.context-path=/management
Run Code Online (Sandbox Code Playgroud)
我在Java Config中有这个
@Override
protected void configure( HttpSecurity http ) throws Exception
{
http.csrf().disable();
http.sessionManagement().sessionCreationPolicy( SessionCreationPolicy.STATELESS );
http.authorizeRequests()
.antMatchers( "/api/**" ).hasRole( "READONLY" )
.antMatchers( "/management/**" ).hasRole( "ADMIN" );
SecurityConfigurer<DefaultSecurityFilterChain, HttpSecurity> securityConfigurer = new XAuthTokenConfigurer( userDetailsServiceBean() );
http.apply( securityConfigurer );
}
Run Code Online (Sandbox Code Playgroud)
当我使用浏览器转到下面的任何内容时/api,我会按预期返回403.当去/ management/info例如,我看到JSON被返回,我也期望403.
我也尝试将其添加到我的application.properties文件中:
management.security.role=ADMIN
Run Code Online (Sandbox Code Playgroud)
但这也没有帮助.
DEBUG输出显示:
2014-05-02 10:15:30 DEBUG [localhost-startStop-1] ExpressionBasedFilterInvocationSecurityMetadataSource -
Adding web access control expression 'hasRole('ROLE_READONLY')', for Ant [pattern='/api/**']
2014-05-02 10:15:30 DEBUG [localhost-startStop-1] ExpressionBasedFilterInvocationSecurityMetadataSource -
Adding …Run Code Online (Sandbox Code Playgroud) 将Maven与PMD,Checkstyle和/或Findbugs的各种插件一起使用时,放置自定义规则集文件的推荐位置是什么?
我在Spring Data JPA中使用Spring Boot 1.3.我想使用主键的专用对象来生成早期主键(如实现域驱动设计中所建议的那样).
假设这个实体:
@Entity
public class Book {
@EmbeddedId
private BookId id;
}
Run Code Online (Sandbox Code Playgroud)
和这个值对象:
@Embeddable
public class BookId implements Serializable {
private UUID id;
protected BookId(){} //for hibernate
public BookId( UUID id ) {
this.id = id;
}
public UUID getId() {
return id;
}
}
Run Code Online (Sandbox Code Playgroud)
然后这很好.但是,我想为所有id类创建一个超类,如:
public class EntityUuidId implements Serializable {
private UUID id;
protected EntityUuidId(){} //for hibernate
public EntityUuidId( UUID id ) {
this.id = id;
}
public UUID getId() {
return …Run Code Online (Sandbox Code Playgroud) 很多地方都说明,如果要在MySQL中使用JDBC,并且支持UTF-8,则需要useUnicode=true在JDBC URL中添加。
例如:
但是,我在 Mac OS X 上使用 MySQL 5.7.19 和 mysql-connector-java 5.1.41(由 Spring Boot 1.5.3 引入)在本地尝试了这个。我添加了一个文件/etc/my.cnf:
[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
Run Code Online (Sandbox Code Playgroud)
这确保 MySQL 设置为 UTF-8:
mysql> SHOW VARIABLES LIKE 'character_set%';
+--------------------------+-----------------------------------------------------------+
| Variable_name | Value |
+--------------------------+-----------------------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | …Run Code Online (Sandbox Code Playgroud)