小编dim*_*sli的帖子

java数组改组

我注意到,根据列表的创建方式,底层的int数组会发生变化:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;


public class Shuffling {

    static Integer[] intArr = {1, 2, 3, 4, 5};
    static Random random = new Random(7);
    public static void main(String[] args) {

        List<Integer> intList = new ArrayList<Integer>(Arrays.asList(intArr));
        Collections.shuffle(intList, random);
        System.out.println("List after shuffling: " + intList);
        System.out.println("intArr: " + Arrays.toString(intArr));
        //OUTPUT:
        //List after shuffling: [5, 4, 1, 3, 2]
        //intArr: [1, 2, 3, 4, 5]


        List<Integer> intList2 = Arrays.asList(intArr);
        Collections.shuffle(intList2, random);
        System.out.println("List2 after shuffling: " + intList2); …
Run Code Online (Sandbox Code Playgroud)

java arrays

5
推荐指数
1
解决办法
1万
查看次数

如何与Jackson一起全局定义命名约定

我在Spring上使用Jackson。我有几种这样的方法:

@RequestMapping(value = "/myURL", method = RequestMethod.GET)
public @ResponseBody Foo getFoo()  {
    // get foo
    return foo;
}
Run Code Online (Sandbox Code Playgroud)

序列化的Foo类很大,并且有很多成员。使用注释或自定义序列化程序可以进行序列化。

我唯一不知道的是如何定义命名约定。我想对所有序列化使用snake_case。

那么,如何为全局定义序列化的命名约定?

如果不可能的话,则必须采用本地解决方案。

java spring jackson spring-boot

4
推荐指数
2
解决办法
4920
查看次数

System.setIn()重新分配最终的System.in

可能重复:
java:"final"System.out,System.in和System.err?

这件事发生在我身上,虽然我已经多次这样做而没有引起注意:System.setIn()重新分配静态决赛System.in.

我错过了什么吗?如何重新分配最终字段.

java

3
推荐指数
1
解决办法
1143
查看次数

WebMvcConfigurationSupport和WebMvcConfigurerAdapter之间的混淆

我想在Windows中使用WebMvcConfigurerAdapter添加资源处理程序,但是在Linux中它不起作用,因此我添加了WebMvcConfigurationSupport

经过调试和测试后,我发现将在两个OS中都将创建两个bean,但是WebMvcConfigurerAdapter仅在Windows上执行的重写功能,WebMvcConfigurationSupport而仅在Linux上执行的重写功能。

我找不到原因。这两个配置类如下所示:

@Configuration
public class JxWebAppConfigurer  extends WebMvcConfigurerAdapter {
   @Override
   public void addResourceHandlers(ResourceHandlerRegistry registry) {
     registry.addResourceHandler("/**").addResourceLocations("file:"+System.getProperty("user.dir")+"/src/main/webapp/");
     super.addResourceHandlers(registry);
   }
}
Run Code Online (Sandbox Code Playgroud)

这是另一个:

@Configuration
public class JxWebConfiguration extends WebMvcConfigurationSupport {
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
       registry.addResourceHandler("/**").addResourceLocations("file:"+System.getProperty("user.dir")+"/src/main/webapp/");
       super.addResourceHandlers(registry);
   }
}
Run Code Online (Sandbox Code Playgroud)

@EnalbeMvc已在主类中添加

java spring spring-mvc

3
推荐指数
1
解决办法
4469
查看次数

JSON 前缀有什么用?

我知道这是一个愚蠢的问题,但我没有找到谷歌预期的答案。Spring MVC 4.3的新功能之一是支持配置JSON前缀

<mvc:message-converters>
    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="jsonPrefix" value=")]}',\n" />
    </bean>
</mvc:message-converters>
Run Code Online (Sandbox Code Playgroud)

因此 JSON 输入/输出可能如下所示:

)]}',\n"{\"userName\":\"ABC\",\"emailId\":\"ABC@gmail.com\"}"
Run Code Online (Sandbox Code Playgroud)

我知道这是出于安全目的。但具体是关于什么呢?

java spring json spring-mvc

3
推荐指数
1
解决办法
5876
查看次数

如何配置 Spring 计划任务以在具有特定延迟的时间范围内运行?

我需要设置春季预定时间从下午 5 点到早上 8 点每 15 分钟执行一次,如何指定这样的表达式?而且我希望任务不仅在周一至周五在工作日执行,而且根据我对 isBusinessDay 逻辑的实现。

java cron spring scheduled-tasks

3
推荐指数
1
解决办法
3262
查看次数

Thymeleaf + Spring-Boot - 为什么我无法访问静态资源?

我的项目树如下所示:

在此输入图像描述

我现在可以访问模板,但无法加载 CSS、图像和 JS 等静态资源。

我有一个common.html片段,在其中声明所有静态资源,例如:

<link rel="stylesheet" th:href="@{/css/app.css}"/>
Run Code Online (Sandbox Code Playgroud)

我包含的标题片段common.html如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

<head th:include="fragments/common :: commonFragment" lang="en"></head>
// page body
</html>
Run Code Online (Sandbox Code Playgroud)

default.html布局文件:

<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymeleaf.org"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">

<head th:include="fragments/common :: commonFragment" lang="en">
    <meta charset="utf-8"/>
    <meta name="robots" content="noindex, nofollow"/>
    <title>Touch</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta http-equiv="content-dataType" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <link rel="shortcut icon" th:href="@{/static/images/favicon.ico}"  type="image/x-icon" />
</head>

<body>
<div th:id="defaultFragment" th:fragment="defaultFragment" class="container">
    <div id="header" th:replace="fragments/header :: headerFragment" …
Run Code Online (Sandbox Code Playgroud)

css spring static-resource thymeleaf spring-boot

3
推荐指数
1
解决办法
8335
查看次数

@Value 中的 SpEL 重构(加密属性)

我有一个Spring Security配置,我必须在其中使用加密属性。我有实用程序静态方法,PasswordUtil.decode()通过它我可以解码一个属性以供进一步使用。

以下解决方案有效,但特定的SpEL对我来说看起来很丑陋。所以,我的问题是否可以将给定的SpEL表达式重构为更好/更短/惯用的东西?

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Value("#{T(my.package.PasswordUtil).decode('${signkey.password}')}")
    private String signKeyPassword;

}
Run Code Online (Sandbox Code Playgroud)

java spring spring-security spring-el

3
推荐指数
1
解决办法
1372
查看次数

jdbcTemplate 执行查询后如何在 Spring Boot 中关闭连接?

在 Spring Boot 中,jdbcTemplate 在执行查询后不会自动关闭一次连接吗?在这种情况下,我正在使用 jdbcTemplate(它连接到 teradata 的位置)执行查询,但在执行查询后会话并未关闭。如何关闭会话?

这是我的 dao 文件 -

@Component
public class DDLReviewDao { 

    @Autowired
    @Qualifier("devbJdbc")
    private JdbcTemplate jdbcTemplate;

    public static final Logger logger = LogManager.getLogger(DDLReviewDao.class);

    public List<DDLObject> getDDLReviewData(DDLQuery ddlQuery) {

        String selectSql = MacroGenerator.generateMacro(ddlQuery);                  
        List<DDLObject> ddlObject = jdbcTemplate.query(selectSql, new DDLMapper());                 
        logger.info(ddlObject);
        return ddlObject;
    }

}
Run Code Online (Sandbox Code Playgroud)

java spring jdbc jdbctemplate spring-boot

2
推荐指数
1
解决办法
2万
查看次数

如何使用Optional来检查java 8中的空值?

我有一个包含嵌套和内部类的类.在某些程序中,我使用遗留java方式中的相等运算符检查对象的空值.以下是我的代码片段:

class Outer {
    Nested nested;
    Nested getNested() {
        return nested;
    }
}
class Nested {
    Inner inner;
    Inner getInner() {
        return inner;
    }
}
class Inner {
    String foo;
    String getFoo() {
        return foo;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我如何对引用变量进行空检查:

Outer outer = new Outer();
if (outer != null && outer.nested != null && outer.nested.inner != null) {
    System.out.println(outer.nested.inner.foo);
}
Run Code Online (Sandbox Code Playgroud)

我知道这也可以使用java 8的Optional类来完成,但是我没有办法在上面的特定场景中做同样的事情.有什么建议吗?

java optional java-8 java-stream

2
推荐指数
2
解决办法
3963
查看次数

DataJpaTest无法在单元测试中更新

我试图找出为什么我的单元测试不起作用。我看到Hibernate在更新之前进行了插入。为什么这样做呢?是测试失败的原因吗?

我已经为测试环境设置了hsqldb,该服务在mysql中似乎可以正常工作。

@Repository
public interface UserDataRepository extends CrudRepository<UserData, Integer> {

    @Transactional
    @Modifying
    @Query("UPDATE UserData ud SET chips = chips + :delta  WHERE ud.id = :userId")
    void addChipsToUser(@Param("userId") int userId, @Param("delta") long delta);
}
Run Code Online (Sandbox Code Playgroud)

我的测试课:

@RunWith(SpringRunner.class)
@DataJpaTest
public class TestJPASlice {

    @Autowired
    private TestEntityManager entityManager;

    @Autowired
    private UserDataRepository repository;

    @Test
    public void testAddChipsToUser() {
            UserData data = new UserData();
            data.setChips(100);
            data.setId(13);                
            this.entityManager.persist(data);                

            System.err.println("pre u");                
            this.repository.addChipsToUser(13, 500);                
            System.err.println("post u");

            UserData two = this.repository.findOne(13);
            assertThat(two.getId()).isEqualTo(13);
            assertThat(two.getChips()).isEqualTo(600);
    }
Run Code Online (Sandbox Code Playgroud)

这是我得到的输出:

Hibernate: update user_player_data set chips=chips+? …
Run Code Online (Sandbox Code Playgroud)

junit spring hibernate spring-data-jpa spring-boot

2
推荐指数
1
解决办法
1139
查看次数

使用骆驼生产者模板和 http 时没有得到 http 响应正文

我正在尝试使用http://camel.apache.org/http.html 上指定的 producertemplate 调用我的简单 GET rest 服务调用。我在这里以 google.com 为例。这是来自未在任何容器上运行的独立客户端。我在这里没有做什么?

SpringCamelContext camelcontext = (SpringCamelContext) springContext.getBean("camelcontextbean");

ProducerTemplate template = camelcontext.createProducerTemplate();
camelcontext.start();
Exchange exchange = template.send("http://www.google.com/search", new Processor() {
            public void process(Exchange exchange) throws Exception {
                exchange.getIn().setHeader(Exchange.HTTP_QUERY, "hl=en&q=activemq");
            }
   });

   Message out = exchange.getOut();
System.out.println("Response from http template is "+exchange.getOut().getBody());
   System.out.println("status header is "+out.getHeader(Exchange.HTTP_RESPONSE_CODE));
Run Code Online (Sandbox Code Playgroud)

我没有得到任何回应。输出是:

来自 http 模板的响应为空

状态头为空

java spring apache-camel

1
推荐指数
1
解决办法
9771
查看次数