Spring

Spring-boot: register mongodb custom converter

烈酒焚心 提交于 2021-02-09 11:08:35
问题 I'm using this dependency into my spring bott service: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> I've created a custom converter: import org.springframework.core.convert.converter.Converter; @Component public class ReferenceWriterConverter implements Converter<Reference, DBObject> { @Override public DBObject convert(Reference reference) { DBObject dbObject = new BasicDBObject(); //... return dbObject; } } I

Run another function when an annotated method is called

女生的网名这么多〃 提交于 2021-02-09 10:58:08
问题 Is there a way to create a custom, or use an existing, annotation to trigger code to run when the annotated method is called? Preferably, I would like to use Spring libraries. For example: @SendEmail("templateName") public void doSomething() { log.info("Something is happening"); } public void sendEmail(String templateName) { // This method is called everytime doSomething() is called log.info("Sending email using template " + templateName); } 回答1: @Component @Aspect public class Mail { @After(

Run another function when an annotated method is called

天涯浪子 提交于 2021-02-09 10:54:33
问题 Is there a way to create a custom, or use an existing, annotation to trigger code to run when the annotated method is called? Preferably, I would like to use Spring libraries. For example: @SendEmail("templateName") public void doSomething() { log.info("Something is happening"); } public void sendEmail(String templateName) { // This method is called everytime doSomething() is called log.info("Sending email using template " + templateName); } 回答1: @Component @Aspect public class Mail { @After(

I'm getting FileNotFoundException while running a war

旧城冷巷雨未停 提交于 2021-02-09 10:51:23
问题 Here is a code line, which I use to access xml file. Contacts contactsEntity = (Contacts) um.unmarshal(new FileReader(new File(classLoader.getResource("Contacts.xml").getFile()))); Here is what I get while running a war: java.io.FileNotFoundException: file:\D:\apache-tomcat-8.0.50\webapps\pb\WEB-INF\lib\phonebook-server-0.0.1-SNAPSHOT.jar!\Contacts.xml (The filename, directory name, or volume label syntax is incorrect) P.S. It is 100% not an issue with file access, because I made a simple

如何使用SpringBoot AOP 记录操作日志、异常日志?

冷暖自知 提交于 2021-02-09 09:56:10
点击上方“ 全栈程序员社区 ”,星标公众号 重磅干货,第一时间送达 作者: 咫尺的梦想_w cnblogs.com/wm-dv/p/11735828.html 平时我们在做项目时经常需要对一些重要功能操作记录日志,方便以后跟踪是谁在操作此功能;我们在操作某些功能时也有可能会发生异常,但是每次发生异常要定位原因我们都要到服务器去查询日志才能找到,而且也不能对发生的异常进行统计,从而改进我们的项目,要是能做个功能专门来记录操作日志和异常日志那就好了。 当然我们肯定有方法来做这件事情,而且也不会很难,我们可以在需要的方法中增加记录日志的代码,和在每个方法中增加记录异常的代码,最终把记录的日志存到数据库中。听起来好像很容易,但是我们做起来会发现,做这项工作很繁琐,而且都是在做一些重复性工作,还增加大量冗余代码,这种方式记录日志肯定是不可行的。 我们以前学过Spring 三大特性,IOC(控制反转),DI(依赖注入),AOP(面向切面),那其中AOP的主要功能就是将日志记录,性能统计,安全控制,事务处理,异常处理等代码从业务逻辑代码中划分出来。今天我们就来用springBoot Aop 来做日志记录,好了,废话说了一大堆还是上货吧。 一、创建日志记录表、异常日志表,表结构如下: 操作日志表 异常日志表 二、添加Maven依赖 < dependency > < groupId > org

Json string from LocalDateTime(java 8) in Spring MVC

徘徊边缘 提交于 2021-02-09 07:10:59
问题 have trouble in LocalDateTime(Java 8) Formatting in Spring MVC framework my VO is like under code in mySQL w_date field is DATETIME and recode like "2015-12-25 23:18:22" public class HistoryBoard { @JsonFormat(pattern="yyyy-MM-dd") @DateTimeFormat(iso = DateTimeFormat.ISO.TIME) private LocalDateTime w_date; public LocalDateTime getW_date() { return w_date; } public HistoryBoard setW_date(String w_date) { DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S"); this.w_date

Why doesn't Spring's PathMatchingResourcePatternResolver match “*” with nothing?

試著忘記壹切 提交于 2021-02-09 07:09:01
问题 I am trying to get a properties file from within a zip file. I need to use a wild card, because I will want to match either "my.properties" or "my_en.properties". I create a ResourcePatternResolver like so: ClassLoader loader = MyClass.class.getClassLoader(); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(loader); I succeed when I try to retrieve the "my.properties" file using no wild card: resolver.getResource("file:C:/somePath/a.zip/META-INF/my.properties")

Json string from LocalDateTime(java 8) in Spring MVC

最后都变了- 提交于 2021-02-09 07:08:38
问题 have trouble in LocalDateTime(Java 8) Formatting in Spring MVC framework my VO is like under code in mySQL w_date field is DATETIME and recode like "2015-12-25 23:18:22" public class HistoryBoard { @JsonFormat(pattern="yyyy-MM-dd") @DateTimeFormat(iso = DateTimeFormat.ISO.TIME) private LocalDateTime w_date; public LocalDateTime getW_date() { return w_date; } public HistoryBoard setW_date(String w_date) { DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S"); this.w_date

Why doesn't Spring's PathMatchingResourcePatternResolver match “*” with nothing?

只愿长相守 提交于 2021-02-09 07:05:15
问题 I am trying to get a properties file from within a zip file. I need to use a wild card, because I will want to match either "my.properties" or "my_en.properties". I create a ResourcePatternResolver like so: ClassLoader loader = MyClass.class.getClassLoader(); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(loader); I succeed when I try to retrieve the "my.properties" file using no wild card: resolver.getResource("file:C:/somePath/a.zip/META-INF/my.properties")

Json string from LocalDateTime(java 8) in Spring MVC

折月煮酒 提交于 2021-02-09 07:02:33
问题 have trouble in LocalDateTime(Java 8) Formatting in Spring MVC framework my VO is like under code in mySQL w_date field is DATETIME and recode like "2015-12-25 23:18:22" public class HistoryBoard { @JsonFormat(pattern="yyyy-MM-dd") @DateTimeFormat(iso = DateTimeFormat.ISO.TIME) private LocalDateTime w_date; public LocalDateTime getW_date() { return w_date; } public HistoryBoard setW_date(String w_date) { DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S"); this.w_date