我必须在SpringBoot应用程序中使用@PathValiable从URL获取params.这些参数常常有斜线.我无法控制用户在URL中输入的内容,因此我希望获得他输入的内容然后我可以处理它.
我已经在这里查看了材料和答案,我不认为对我来说好的解决方案是要求用户以某种方式编码输入的参数.
SpringBoot代码很简单:
@RequestMapping("/modules/{moduleName}")
@ResponseBody
public String moduleStrings (@PathVariable("moduleName") String moduleName) throws Exception {
...
}
Run Code Online (Sandbox Code Playgroud)
所以URL例如如下:
http://localhost:3000/modules/...
Run Code Online (Sandbox Code Playgroud)
问题是param moduleName经常有斜杠.例如,
metadata-api\cb-metadata-services OR
app-customization-service-impl\\modules\\expand-link-schemes\\common\\app-customization-service-api
Run Code Online (Sandbox Code Playgroud)
因此用户可以定义输入:
http://localhost:3000/modules/metadata-api\cb-metadata-services
Run Code Online (Sandbox Code Playgroud)
这可能是在/ modules /之后获取用户在URL中输入的所有内容吗?
如果有人告诉我处理这个问题的好方法是什么.
我有一个带有服务器 URL的app-const.ts:
export class AppConst {
public static serverPath = 'http://10.0.0.126:3031';
}
Run Code Online (Sandbox Code Playgroud)
这是 Spring Boot REST 服务器的 URL 路径。在这种情况下,我将这个常量放在一个地方,并在所有模块中使用它。但是 ,在构建之后,如果服务器 URL 发生更改,我将无法在不重新构建整个项目的情况下更改此常量。
有什么方法可以将这个常量保存在主机上的某个外部配置文件中(在 index.html 旁边),以便我可以在不重建项目的情况下更改它(比如Spring Boot 中的application.properties文件,谁知道)?
或者我如何通过更改服务器 URL 轻松管理这种情况?
另外。清除情况:我将我的 Angular 网络客户端放在主机上。然后这个客户端开始与可以放置在某处(例如在云中)的 Spring Boot REST 服务器通信。这个 Spring Boot 服务器有一个服务器 URL (serverPath),有时可能会更改。现在,如果服务器 URL 更改,我需要更改此 serverPath 常量并仅由于此常量而重建整个 Angular 项目。
我的 Angular Universal 项目在我的本地主机上运行。所以现在我想在安装了 Node.js 的标准虚拟主机上测试它。
我跑了:
npm run build
Run Code Online (Sandbox Code Playgroud)
并收到带有客户端和服务器子文件夹的dist文件夹。
我应该如何使用这些文件夹和文件来在主机上运行项目?
谢谢。
我想了解我是否应该自己手动清理内存中的原型bean.
在Spring文档中,您可以看到: "客户端代码必须清理原型范围的对象,并释放原型bean所持有的昂贵资源."
因此,您似乎应该自己清理原型bean.
然而.
我正在使用VisualVM 内存分析器.我创建了许多原型bean.你可以看到它们的51个实例.
然后你可以看到垃圾收集器清理内存时的情况- 所有原型bean都被清除了.
所以有人可以澄清这种情况吗?原型bean是否被垃圾收集器成功清除,或者我们应该手动清除它们(如果是,如何)?
加成. 有些人要求展示创建原型bean的代码.实际上我没有看到任何意义,因为在特定的例子中我只是作为测试创建它们,这与从内存中清理原型bean的实际情况无关.开发人员可以通过不同的方式创建原型bean,但它们的未来行为不会或不应该依赖于创建方法.
在我们的真实项目中,我们有超过400个组件注释为Prototype-beans,我可以看到相同的行为.我已经向我们的系统提出了许多请求,通过VisualVM查看了一些创建的原型bean,然后在垃圾收集器清理内存后,所有原型bean都被清除.
我展示测试代码只是希望那些提出这个问题的人会给出关于真实行为的一些有意义的信息,而不仅仅是关于这个特定测试bean创建的空话.
测试为示例创建原型bean:
for(int i=0;i<10;i++){
Prototype1 prototype1=applicationContext.getBean(Prototype1.class);
Prototype2 prototype2=applicationContext.getBean(Prototype2.class);
prototype1.test1();
prototype2.test2();
}
Run Code Online (Sandbox Code Playgroud)
原型bean的测试示例:
@Service
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class Prototype1 {
private int number;
public Prototype1() {
System.out.println("Prototype1 was created.");
}
public void test1(){
}
}
Run Code Online (Sandbox Code Playgroud) 有人可以说这是一个重复的问题,但我查看了很多答案,尝试了很多方法但无法理解我错过了什么。
我在我的 REST 服务器中使用了非常基本的 Spring Security。当我第一次直接从浏览器向我的服务器发出请求到localhost:.../getData我当然会被要求授权。
然后服务器每次都允许这个请求。我如何注销以便下一个请求再次需要授权?
现在我尝试在我的服务器上使用几种方法来注销:
@RequestMapping(value = "/logoutMe2", method = RequestMethod.GET)
public void logout2() {
SecurityContextHolder.getContext().setAuthentication(null);
}
@RequestMapping(value = "/logoutMe3", method = RequestMethod.GET)
public void logout3() {
SecurityContextHolder.clearContext();
}
@RequestMapping(value = "/logoutMe", method = RequestMethod.GET)
public void logout(HttpServletRequest rq, HttpServletResponse rs) {
SecurityContextLogoutHandler securityContextLogoutHandler =
new SecurityContextLogoutHandler();
securityContextLogoutHandler.logout(rq, rs, null);
}
@RequestMapping(value = "/logoutMe4", method = RequestMethod.GET)
public static void myLogoff(HttpServletRequest request, HttpServletResponse response) {
CookieClearingLogoutHandler cookieClearingLogoutHandler = new CookieClearingLogoutHandler(AbstractRememberMeServices.SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY);
SecurityContextLogoutHandler securityContextLogoutHandler = …Run Code Online (Sandbox Code Playgroud) 在我的示例中,理论上 2 种方法的性能应该非常相似。在第一种情况下,我使用数组,在第二种情况下 - ArrayList 具有保证的容量。
结果如下:
LessonBenchmark2.capacityTestArray avgt 5 1,354 ± 0,057 ms/op
LessonBenchmark2.capacityTestArrayListEnsured avgt 5 32,018 ± 81,911 ms/op
这里似乎数组要快得多(1.354 vs 32.018 ms/op)。可能是我的 JMH 基准测试设置不正确。如何做对?
此外,如果我使用 @Setup(Level.Invocation),那么结果很接近(1,405 对 1,496 ms/op):
LessonBenchmark.capacityTestArray avgt 5 1,405 ± 0,143 ms/op
LessonBenchmark.capacityTestArrayListEnsured avgt 5 1,496 ± 0,104 ms/op
但是据说要小心使用 Invocation 。此外,迭代模式在逻辑上似乎是正确的。
这是代码:
public static void main(String[] args) throws Exception {
org.openjdk.jmh.Main.main(args);
}
static final int iter = 5;
static final int fork = 1;
static final int warmIter = 5;
@State(Scope.Benchmark)
public …Run Code Online (Sandbox Code Playgroud) 使用新的 API,我可以获得本地日期和时间:
LocalTime localTime = LocalTime.of(9,0,0);
LocalDate localDate = LocalDate.of(2017, Month.JUNE, 3);
LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
System.out.println("LocalDateTime:" + localDateTime);
Run Code Online (Sandbox Code Playgroud)
如果我需要将时间转换为特定时区,我也可以使用 ZoneId:
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("GMT"));
System.out.println("ZonedDateTime: " + zonedDateTime);
ZonedDateTime zonedDateTime2 = ZonedDateTime.of(localDateTime, ZoneId.of("Europe/London"));
System.out.println("ZonedDateTime London: " + zonedDateTime2);
Run Code Online (Sandbox Code Playgroud)
我可以通过以下方式获取当前时间:
Instant currentTime = Instant.now();
Run Code Online (Sandbox Code Playgroud)
我的问题。是否可以使用这个新 API 获取客户端计算机上使用的当前时间的ZoneId 和数字定义(例如 +04:00)?
我们可以从时钟创建即时。时钟有时区。
Clock clock1 = Clock.system(ZoneId.of("Europe/Paris"));
Clock clock2 = Clock.system(ZoneId.of("Asia/Calcutta"));
System.out.println("Clock1 instant: " + clock1.instant());
System.out.println("Clock2 instant: " + clock2.instant());
Run Code Online (Sandbox Code Playgroud)
输出给出相同的瞬间:
时钟 1 瞬时:2022-01-21T18:36:21.848Z
时钟 2 瞬时:2022-01-21T18:36:21.848Z
那么在 Clock 中设置时区的目的是什么?
我已经浏览了许多关于entityManager.flush()方法的主题。在我的实践中,我一直使用persist()和commit()方法。
我还发现,有时flush()在对数据库的选择请求期间自动执行,此时它会检查例如数据库的约束,因此如果由于选择期间的约束而导致持久化对象错误,则会抛出异常。
其实我想明白:
当您执行flush()方法时,持久化数据是否会保存在数据库中?所以你不需要在flush()之后做commit ()吗?
在某些具体情况下,使用flush()而不是commit() 有什么好处?
我必须在 Postgres 中使用包含大写和小写字母的表名和列名。例如OrgInfo
我想使用Hibernate创建这样的表和列。如果我使用标准方法:
@Entity
@Table(name = "OrgInfo")
Run Code Online (Sandbox Code Playgroud)
然后我收到名为“org_info”的表。
如何配置 Hibernate 创建具有精确给定名称(包括大写和小写字母)的表和列?
我尝试过使用
@Entity
@Table(name = "\"OrgInfo\"")
Run Code Online (Sandbox Code Playgroud)
没有效果。
我也使用 Spring Boot 并尝试像这样配置:
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.DefaultNamingStrategy 或 spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
java ×8
spring ×2
angular ×1
clock ×1
constants ×1
datetime ×1
deployment ×1
file ×1
hibernate ×1
javabeans ×1
jmh ×1
jpa ×1
performance ×1
postgresql ×1
prototype ×1
rest ×1
spring-boot ×1
spring-mvc ×1
timezone ×1
url ×1
web-hosting ×1