spring – Save oauth/token body into Java object

After I successfully run the request to http://IP:port/oauth/ token to get the authorization token from oauth using spring framework, the response body looks something like this:

{
     access_token = jsjxjdnjf .... some_acces_token,
     token_type: bearer,
     (....) more fields
}

The client to acces this endpoint is a simple Java app using org.springframework.web.client.RestTeplate
My question is:
Is there a predefined class that allows me to encapsulate(map) that information and access it through getters?
Or I have to implement it myself, which would look like this:

public class OauthTokeWrapper {
private String access_token;
(...)//getters,constructors...
}

java – Erro na exibição de Health Check no Swagger – Spring Boot

Estou com um projeto em Spring Boot, Spring Actuator e Swagger. Quero deixar a rota de Health Check disponível no Swagger. Fiz a configuração do mesmo e apareceu a rota do Health Check como GET, porém com um body que não consigo tirar. Toda vez que eu faço a requisição ele dar erro por conta desse body indesejado.

Eu vi alguns tópicos aqui no Stack Overflow e Github e parece que é um bug.

Alguém conhece uma solução que possa funcionar?

id 'org.springframework.boot' version '2.4.0'

/** health check */
implementation 'org.springframework.boot:spring-boot-starter-actuator'

/** swagger */
implementation('io.springfox:springfox-swagger2:2.9.2')
implementation('io.springfox:springfox-swagger-ui:2.9.2')

Java 11

application.yml:

management:
  health:
    diskSpace:
      enabled: false
    probes:
      enabled : true
  endpoints:
    health:
      sensitive : false
    web:
      base-path: "https://pt.stackoverflow.com/"
      exposure:
        include: "health"
  endpoint:
    health:
      show-details : always

  springdoc:
    show-atuator : true

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

Mesmo apagando o conteúdo do body na mão, o mesmo erro ocorre.

How to maintain HashiCorp vault tokens and secrets in spring boot using Vault Agent

I’m using(just as client) the Vault as Service using REST calls for encrypting the data

Able to login to vault using role-id and secret and got the token
As you all aware the token will expire in 15 minutes and secret will expire in 30 days

I am deploying my spring boot application in open-shift.

I have to integrate Vault in multiple applications all are in same namespace on openshift?
if i maintain at application level i have multiple pods running for one application as these pods are stateless i’m having more active secrets (i’m running 20-50 pods based on application) which is at risk…

Currently for token renewing i’m running a scheduler every 15 minutes.

I’m looking for approach it maintains 1 active secret all the time for all applications.
How do i maintain the secret in better way?

What is vault Agent and how it works?

NOTE: Vault is not installed in my server, it was an external application

java – Spring Boot should we always use interfaces even with one implementation

quick question. I’m using Spring Boot and I created e.g. this class

public interface ProductService {

    Page<Product> getPage(Pageable pageable);

}

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductRepository productRepository;

    @Override
    public Page<Product> getPage(String name, Pageable pageable) {
        return productRepository.findAll(pageable);
    }
}

And this interface has only one implementation. And what is better to use interface with one implementation and inject interface or just create a class without interface and inject class?

How to test if a list parameter is empty in a JPQL Spring Data JPA @Query?

I’d like to retrieve categories of a given list, but all categories if the list is empty, using an OR condition. Something like that:

@Query("SELECT obj FROM Category obj WHERE (:categories IS EMPTY) OR (obj IN :categories)")
List<Category> find(List<Category> categories);

In other words, I’d like do know how to test if a list parameter is empty.

Error while using Logback in spring boot application

What am I are trying to do???

I am adding a logger in my spring boot application using Logback.

What I have done so far?

I added the below in application properties

logging.config=classpath:logback-dev.xml

And this is my logback configuration

logback-dev.xml
<configuration>
    <Appenders>
        <!-- Console Appender -->
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                pattern="%d{yyyy-MMM-dd HH:mm:ss a} (%t) %-5level %logger{36} - %msg%n" />
        </Console>
        <!-- File Appender -->
        <File name="File"
            fileName="D:/logs/myApp.log">
            <PatternLayout
                pattern="%d{yyyy-MMM-dd HH:mm:ss a} (%t) %-5level %logger{36} - %msg%n" />
        </File>
         <encoder>
            <pattern>%d{yyyyMMdd'T'HHmmss} %thread %level %logger{15} %msg%n</pattern>
        </encoder>
    </Appenders>
    <Loggers>
        <logger name="com.test" level="DEBUG"
            additivity="false">
            <AppenderRef ref="Console" />
            <AppenderRef ref="File" />
        </logger>
    </Loggers>
</configuration>

While running the application, I got the below exception.

Logging system failed to initialize using configuration from 'classpath:logback-dev.xml'
java.lang.IllegalStateException: Logback configuration error detected:
ERROR in ch.qos.logback.core.joran.spi.Interpreter@3:13 - no applicable action for (Appenders), current ElementPath  is ((configuration)(Appenders))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@5:47 - no applicable action for (Console), current ElementPath  is ((configuration)(Appenders)(Console))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@7:79 - no applicable action for (PatternLayout), current ElementPath  is ((configuration)(Appenders)(Console)(PatternLayout))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@11:65 - no applicable action for (File), current ElementPath  is ((configuration)(Appenders)(File))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@13:79 - no applicable action for (PatternLayout), current ElementPath  is ((configuration)(Appenders)(File)(PatternLayout))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@15:13 - no applicable action for (encoder), current ElementPath  is ((configuration)(Appenders)(encoder))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@16:22 - no applicable action for (pattern), current ElementPath  is ((configuration)(Appenders)(encoder)(pattern))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@19:11 - no applicable action for (Loggers), current ElementPath  is ((configuration)(Loggers))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@21:23 - no applicable action for (logger), current ElementPath  is ((configuration)(Loggers)(logger))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@22:33 - no applicable action for (AppenderRef), current ElementPath  is ((configuration)(Loggers)(logger)(AppenderRef))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@23:30 - no applicable action for (AppenderRef), current ElementPath  is ((configuration)(Loggers)(logger)(AppenderRef))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@26:23 - no applicable action for (logger), current ElementPath  is ((configuration)(Loggers)(logger))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@27:33 - no applicable action for (AppenderRef), current ElementPath  is ((configuration)(Loggers)(logger)(AppenderRef))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@28:30 - no applicable action for (AppenderRef), current ElementPath  is ((configuration)(Loggers)(logger)(AppenderRef))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@31:23 - no applicable action for (logger), current ElementPath  is ((configuration)(Loggers)(logger))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@32:33 - no applicable action for (AppenderRef), current ElementPath  is ((configuration)(Loggers)(logger)(AppenderRef))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@33:30 - no applicable action for (AppenderRef), current ElementPath  is ((configuration)(Loggers)(logger)(AppenderRef))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@36:23 - no applicable action for (logger), current ElementPath  is ((configuration)(Loggers)(logger))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@37:33 - no applicable action for (AppenderRef), current ElementPath  is ((configuration)(Loggers)(logger)(AppenderRef))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@38:30 - no applicable action for (AppenderRef), current ElementPath  is ((configuration)(Loggers)(logger)(AppenderRef))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@41:23 - no applicable action for (logger), current ElementPath  is ((configuration)(Loggers)(logger))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@42:33 - no applicable action for (AppenderRef), current ElementPath  is ((configuration)(Loggers)(logger)(AppenderRef))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@43:30 - no applicable action for (AppenderRef), current ElementPath  is ((configuration)(Loggers)(logger)(AppenderRef))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@46:23 - no applicable action for (logger), current ElementPath  is ((configuration)(Loggers)(logger))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@47:33 - no applicable action for (AppenderRef), current ElementPath  is ((configuration)(Loggers)(logger)(AppenderRef))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@48:30 - no applicable action for (AppenderRef), current ElementPath  is ((configuration)(Loggers)(logger)(AppenderRef))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@51:23 - no applicable action for (root), current ElementPath  is ((configuration)(Loggers)(root))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@52:33 - no applicable action for (AppenderRef), current ElementPath  is ((configuration)(Loggers)(root)(AppenderRef))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@53:30 - no applicable action for (AppenderRef), current ElementPath  is ((configuration)(Loggers)(root)(AppenderRef))
        at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:169)
        at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithSpecificConfig(AbstractLoggingSystem.java:66)
        at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:57)
        at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:118)
        at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:311)
        at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:281)
        at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:239)
        at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:216)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
        at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:80)
        at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:53)
        at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:345)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
        at com.shc.ecom.ons.listener.OnsListenerApplication.main(OnsListenerApplication.java:15)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:109)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)
Exception in thread "main" java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:109)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)
Caused by: java.lang.IllegalStateException: java.lang.IllegalStateException: Logback configuration error detected:
ERROR in ch.qos.logback.core.joran.spi.Interpreter@3:13 - no applicable action for (Appenders), current ElementPath  is ((configuration)(Appenders))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@5:47 - no applicable action for (Console), current ElementPath  is ((configuration)(Appenders)(Console))
ERROR in ch.qos.logback.core.joran.spi.Interpreter@7:79 - no applicable action for (PatternLayout), current ElementPath  is ((configuration)(Appenders)(Console)(PatternLayout))

I am stuck here as I am not able to figure out what I did wrong. This is my first spring boot application.
And for reference, this is my POM file

Pom.xml
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-streams</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
            <version>${spring-kafka.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-to-slf4j</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>logback-access</artifactId>
                    <groupId>ch.qos.logback</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>logback-core</artifactId>
                    <groupId>ch.qos.logback</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>logback-classic</artifactId>
                    <groupId>ch.qos.logback</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.8.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
        <dependency>
            <groupId>ca.pjer</groupId>
            <artifactId>logback-awslogs-appender</artifactId>
            <version>1.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

Nao consigo usar httpsession java web 7 spring mvc 4

 @RequestMapping(value = "efectuarLogin", method = RequestMethod.POST)
    public String efectuarLogin(@RequestParam("usuario") String usuario, @RequestParam("senha") String senha, ModelMap model, HttpSession session) {

        LoginDao loginDao = new LoginDao();

        LoginModelo login = new LoginModelo();

        login.setUsuario(usuario);
        login.setSenha(senha);
        loginDao.getUser(login);

        
        if (login.isExiste()) {
            model.addAttribute("usu", login);
            session.setAttribute("usuaio", login);
            //model.put("use",login);
            return "home";
        } else {
            return "login";
        }

    }

Inject property value dynamically in spring boot

As we all know we can define properties in the application.yml file of SpringBoot and read these properties by using @value(“${propertyName}”) in code. To do this we have to define the value of these properties as well. To read the system configuration we can use the Environment class. But I have a different requirement to do the same.

We have a different application who is managing all the properties wrt environment. I have to read these properties from that service and inject here. Is there anyway to do this?

For example in .yml file I want the below values from a different service and inject there.
Spring
t kafka
tt consumer
ttt bootstrap-servers : dynamically I want to set this value at runtime by reading from a
different service

spring – java reflect generated proxy method unknown source

I have been trying to solve an issue, from past couple of days.

I have an interface, impl class already. BtService, BtServiceImpl
introduced a new method into BtService, BtServiceImpl – getRoleNames();

There is a Proxy interface with same name “BtService” in another java spring module.

After I made changes, I am seeing remote invocation resulted in failed exception : BtService.getRoleNames(); ArraysIndexOutofBoundException: Arrays out of range 0. I am really not doing anything inside getRoleNames() except returning “new ArrayList<>();” , intersting thing is I had log statement in this method which never got printed , for me it looks like I am doing something wrong. really not sure.

Thanks for help.
-Sri

Hibernate – how to add table not business related (Spring Boot)

I wonder what is the best practice in my case:
I have table A and I want to add table B that is not business related to table A, so data in table B should be used only to sort data from table A.

So in that case there is a wrong approach to add foreign key in table A to table B.

I see 2 options:

  1. Table B has foreign key to table A but in that case entity A, after fetching data from table A will load also data from table B? I’d like to get data from table B only on demand and I don’t want to have data from table B when I need only data from table A
  2. I have foreign key in table B that points to table A and nothing in entity A so I have to manually get data from table B for some specific row in table A.

My current solution has to be changed because Table A shouldn’t know about TableB.

@Table(name="TableA")
public class A{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)

@OneToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@JoinColumn(name="tablea_id", unique="true)
private ObjectA objA;

}

What’s the best solution in my case? I want to get data from table B only in some rare cases…