小编deg*_*ath的帖子

EmbeddedServletContainerCustomizer在spring boot 2.0中

我尝试将我的应用程序从spring boot 1.5迁移到2.0问题是我找不到EmbeddedServletContainerCustomizer.任何想法如何通过?

@Bean
public EmbeddedServletContainerCustomizer customizer() {
    return container -> container.addErrorPages(new ErrorPage(HttpStatus.UNAUTHORIZED, "/unauthenticated"));
}
Run Code Online (Sandbox Code Playgroud)

更新: 我在org.springframework.boot.autoconfigure.web.servlet包中找到它作为ServletWebServerFactoryCustomizer.

@Bean
public ServletWebServerFactoryCustomizer customizer() {
    return container -> container.addErrorPages(new ErrorPage(HttpStatus.UNAUTHORIZED, "/unauthenticated"));
}
Run Code Online (Sandbox Code Playgroud)

但是有一个错误:无法解决方法

'addErrorPages(org.springframework.boot.web.server.ErrorPage)'

我还必须将新错误页面的导入更改org.springframework.boot.web.servletorg.springframework.boot.web.server

java spring-boot

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

连接到 ALB 的 AWS HTTP API Gateway 返回 503

我当前的解决方案: 我的应用程序由部署到 ECS 的两个单独的服务/容器组成。这些服务位于虚拟私有云 (VPC) 内部,为了公开我创建的 EC2 应用程序负载均衡器(该应用程序运行完美),我可以通过负载均衡器 URL 轻松访问该应用程序。

我想要实现的目标: 目前我正在尝试创建一个链接到上述负载均衡器的API网关,以通过API网关而不是负载均衡器访问应用程序。

为了实现这一目标,我 找到了一个 AWS 教程,它基本上完成了我想要做的事情,所以我一步步学习了本教程

  1. 创建 Http API 网关
  2. 创建VPC-LINK(连接到所有可能的子网和所有可能的安全组,以防万一)
  3. 创建单一路线ANY /{proxy+}来捕获基本上所有的东西
  4. 创建了与该路由的集成(我选择上面提到的负载均衡器作为目标服务)。

一切都是一步一步的,与教程中的相同,但不幸的是最后一步我应该看到我看到的网页ERROR: 503 {"message":"Service Unavailable"}

我还做了什么来解决这个问题:

  • 添加了 Api 网关日志,但我没有收到任何有趣的东西,只有原始数据,如下所示:
{
    "requestId": "PgELwjAyjoEEPgQ=",
    "ip": "185.244.96.51",
    "requestTime": "24/Mar/2022:18:09:40 +0000",
    "httpMethod": "GET",
    "routeKey": "ANY /{proxy+}",
    "status": "503",
    "protocol": "HTTP/1.1",
    "responseLength": "33"
}
Run Code Online (Sandbox Code Playgroud)
  • 使用不同的负载均衡器侦听器(443 或 80)进行播放。
  • 使用不同的安全组(尝试使用与 ALB 相同的安全组,或使用所有可能的安全组)

问题是,我在这里缺少什么? 我猜问题出在VPC链路和负载均衡器之间的连接上,但说实话我不知道如何检查和验证它​​。我一步一步点击了所有内容,在很多地方都有单选选项,所以我真的很困惑我可能在哪里犯了错误。这是基础设施的说明性照片以及我的猜测问题可能出在哪里(但这仍然是猜测。在此输入图像描述

amazon-ec2 amazon-web-services amazon-vpc aws-api-gateway

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

使用 liquibase 的 hibernate-envers

我的问题可能很简单,但是 hibernate-envers 的文档说我只需要 2 个步骤就可以使 hibernate envers 工作:

  1. 类路径上的 hibernate-envers jar,

  2. 实体上的@Audited 注释。

首先我补充说:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-envers</artifactId>
    <version>5.2.12.Final</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

其次,我在实体上方添加了 @Audited 注释:

@Entity
@Audited
@Table(name = "users")
public class User {...}
Run Code Online (Sandbox Code Playgroud)

在这两个步骤之后,我可以选择:

  1. 使用 hbm2ddl 工具生成自动模式

  2. 单独创建模式。

教程/文档等中的很多人都说你应该单独做,所以这就是我的模式的样子(在百里香叶中)。

要手动创建模式,我必须使用一个特殊表(通常称为 revinfo)创建变更集:

    <createTable tableName="revinfo">
        <column name="rev" type="integer">
            <constraints primaryKey="true"/>
        </column>
        <column name="revtstmp" type="bigint"></column>
    </createTable>
Run Code Online (Sandbox Code Playgroud)

现在我只需要添加名为 tablename_AUD 的新表(ofc 我可以通过在我的实体 @AuditTable("new_name") 中添加新注释来更改此名称,但事实并非如此)并添加了 rev 和 revtype 列。

这是我在 liquibase 中的现有表之一:

    <createTable tableName="users">
        <column name="id" type="int">
            <constraints primaryKey="true" nullable="false"/>
        </column>
        <column name="email" type="varchar(200)">
            <constraints unique="true" nullable="false"/>
        </column> …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate hibernate-envers

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

执行javadoc注释

我正在阅读很多关于javadoc的文章,但是当"样板"开始时仍然无法管理.在这个例子中:

/**
 * Returns a list of tasks for specific user
 * @param userId
 * @return Selected list of tasks
 */
List<Task> getTasksForUser(Integer userId);

/**
 * Returns a list of tasks in chosen month and year
 * @param month
 * @param year
 * @return selected list of tasks
 */
List<Task> getTasks(Integer month, Integer year);
Run Code Online (Sandbox Code Playgroud)

我可以以某种方式执行它们以减少样板或我应该删除它们吗?

为什么75%的文章称为"Javadoc的最佳实践,我们有重复?"例如:

/**
 * Returns a list of tasks using params month, year
 * @param month
 * @param year
 * @return a list of tasks
 */ …
Run Code Online (Sandbox Code Playgroud)

java javadoc

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

使用Java中的API改进使用

根据官方网站的信息,我添加了最新的依赖并开始发展.

首先,我创建了我感兴趣的数据模型:

public class Data{
    String parametr1;
    //geters and setters ommited
}
Run Code Online (Sandbox Code Playgroud)

第二步是添加服务:

public interface GitHubService {
    @GET("/repos/{owner}/{repo}")
    Call<Data> repoInfos(@Path("user") String owner, @Path("repo") String repo);

    Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.github.com/").build();
}
Run Code Online (Sandbox Code Playgroud)

第三个是添加实施:

@Service
public class GitHubServiceImpl implements GitHubService {

    final GitHubService gitHubService = GitHubService.retrofit.create(GitHubService.class);

    @Override
    public Call<DetailDto> repoDetails(String owner, String repo) {
        return gitHubService.repoDetails(owner, repo);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是有一个错误:

java.lang.IllegalArgumentException: Could not locate ResponseBody converter for class model.Data.
Run Code Online (Sandbox Code Playgroud)

这是完整的错误日志跟踪

java github github-api retrofit retrofit2

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

RequestParam 设置为单个参数

我用参数创建了端点:

@GetMapping("/me")
public MeDto getInfo(@RequestParam("param") Set<Integer> params) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

一切正常,但我需要单独发送 ID,例如

/me?param=1&param=2
Run Code Online (Sandbox Code Playgroud)

有没有办法使它成为:

/me?param=1,2...N
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?谢谢。

java parameters spring

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

更改 websocket 范围(从应用程序到会话/视图)

我用教程创建了一个基本的网络套接字。

这是一个配置:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
         registry.addEndpoint("/chat");
         registry.addEndpoint("/chat").withSockJS();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是消息处理控制器:

@MessageMapping("/chat")
@SendTo("/topic/messages")
public OutputMessage send(Message message) throws Exception {
    return new OutputMessage("Hello World!");
}
Run Code Online (Sandbox Code Playgroud)

一切正常,但根据我的调查,默认情况下 WebSockets 看起来有一个应用程序范围(通过连接到通道,我可以看到来自所有用户的所有调用)。

我想要做的是只能看到来自当前用户会话或当前视图的调用。关于如何应用这些配置的任何想法?

java websocket spring-boot spring-messaging spring-websocket

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

通过java 8接口改进map中的containsKey

我有一个名为Time with fields的实体/表单:

Integer id;
Date date;
Integer value;
Run Code Online (Sandbox Code Playgroud)

另一种称为SummaryForm的形式

String title;
String descr;
List<Object> newList;
Run Code Online (Sandbox Code Playgroud)

如何使用Java 8接口提高地图使用率.现在我有一个if语句:

if (map.containsKey(key)) {
            map.get(key).newList().add(new SummaryForm(time.getDate(), time.getValue()));
        }
Run Code Online (Sandbox Code Playgroud)

是否有任何简单的方法来改进它以避免if语句,使用map.containsKey?在我看来,我需要像putIfPresent这样的东西,但似乎只有putIfAbsent方法.

java dictionary java-8

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

在Spring Repository中命名查询参数

这个例子很好用。

@Query("select t from TimeTable t where MONTH(t.date) = ?1 and YEAR(t.date) = ?2")
List<TimeTable> findAll(Integer month, Integer year);
Run Code Online (Sandbox Code Playgroud)

现在我尝试用下面的名称替换?1和?2

@Query("select t from TimeTable t where MONTH(t.date) =month and YEAR(t.date) =:year")
List<TimeTable> findAll(Integer month, Integer year);
Run Code Online (Sandbox Code Playgroud)

这不起作用并产生错误:

org.springframework.dao.InvalidDataAccessApiUsageException:该位置为[1]的参数不存在;嵌套异常是java.lang.IllegalArgumentException:具有该位置[1]的参数不存在

还有这个

@Query("select t from TimeTable t where MONTH(t.date) =:month and YEAR(t.date) =:year")
List<TimeTable> findAll(Integer month, Integer year);
Run Code Online (Sandbox Code Playgroud)

产生错误:

org.springframework.dao.InvalidDataAccessApiUsageException:参数绑定的名称不能为null或为空!在JDK <8上,您需要对命名参数使用@Param,在JDK 8或更高版本上,请确保使用-parameters进行编译。嵌套异常为java.lang.IllegalArgumentException:参数绑定的名称不能为null或为空!在JDK <8上,您需要对命名参数使用@Param,在JDK 8或更高版本上,请确保使用-parameters进行编译。

更新:

就像上面的错误所示,我不需要在JDK 8上使用@Param,但是使用@Param的解决方案有效:

List<TimeTable> findAll(@Param("month") Integer month, @Param("year") Integer year);
Run Code Online (Sandbox Code Playgroud)

当我删除@Params时,它将再次出现此错误。

java spring jpa jpql spring-data-jpa

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

我应该测试Spring Boot Application的main()方法吗?

当我创建Spring Boot Application时,它会生成2个类:

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

第二个是测试:

@RunWith(SpringRunner.class)
@SpringBootTest
public class AppTest {

    @Test
    public void contextLoads() {
    }

}
Run Code Online (Sandbox Code Playgroud)
  1. 就像您可以注意到contextLoads测试为空。我应该如何为contextLoad提供正确的测试?应该留空吗?这是对的吗?还是我应该在里面放东西?

更新:

  1. 为什么这个测试应该保留?如果我在应用程序中有更多测试,我将知道spring上下文是否已加载或否。不仅仅是过度

我喜欢readed回答这个,但它并没有给我一个明确的答复。

java testing spring spring-boot

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