Spring-cloud-microservices - spring-cloud-netflix

I am using camden-sr2 release to build spring cloud microservices,was following one of the video "building bootiful microservices" by Josch. I'm trying to integrate spring cloud stream using rabbitmq and getting error: Here is my pom.xml file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>reservation-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>reservation-client</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
bootstrap.properties:
spring.application.name=reservation-client
spring.cloud.config.uri=http://localhost:8888
reservation-client.properties:
server.port=${PORT:9999}
spring.cloud.stream.bindings.output.destination=reservations
Java Files:
#EnableBinding(ProducerChannel.class)
#EnableCircuitBreaker
#EnableZuulProxy
#EnableDiscoveryClient
#IntegrationComponentScan
#SpringBootApplication
public class ReservationClientApplication {
public static void main(String[] args) {
SpringApplication.run(ReservationClientApplication.class, args);
}
}
public interface ProducerChannel {
#Output
MessageChannel output();
}
#RestController
public class ReservationController {
#Autowired
private ProducerChannel producer;
#PostMapping(path = "/reservationSave")
public void sendReservationDetail(/*#RequestBody Reservation reservation*/){
System.out.println("request received");
Reservation reservation=new Reservation();
reservation.setReservationName("Chennai");
Message<Reservation> message= MessageBuilder.withPayload(reservation).build();
producer.output().send(message);
System.out.println("Message Sent to RabbitMq");
}
}
That's it and part of error message that I get is:
=========================
AUTO-CONFIGURATION REPORT
Positive matches:
ConfigServiceBootstrapConfiguration#configServicePropertySource matched:
- #ConditionalOnProperty (spring.cloud.config.enabled) matched (OnPropertyCondition)
ConfigurationPropertiesRebinderAutoConfiguration matched:
- #ConditionalOnBean (types: org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor; SearchStrategy: all) found bean 'org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor' (OnBeanCondition)
ConfigurationPropertiesRebinderAutoConfiguration#configurationPropertiesBeans matched:
- #ConditionalOnMissingBean (types: org.springframework.cloud.context.properties.ConfigurationPropertiesBeans; SearchStrategy: current) did not find any beans (OnBeanCondition)
ConfigurationPropertiesRebinderAutoConfiguration#configurationPropertiesRebinder matched:
- #ConditionalOnMissingBean (types: org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder; SearchStrategy: current) did not find any beans (OnBeanCondition)
EncryptionBootstrapConfiguration matched:
- #ConditionalOnClass found required class 'org.springframework.security.crypto.encrypt.TextEncryptor' (OnClassCondition)
PropertyPlaceholderAutoConfiguration#propertySourcesPlaceholderConfigurer matched:
- #ConditionalOnMissingBean (types: org.springframework.context.support.PropertySourcesPlaceholderConfigurer; SearchStrategy: current) did not find any beans (OnBeanCondition)
Negative matches:
ConfigServiceBootstrapConfiguration.RetryConfiguration:
Did not match:
- #ConditionalOnProperty (spring.cloud.config.failFast) did not find property 'spring.cloud.config.failFast' (OnPropertyCondition)
Matched:
- #ConditionalOnClass found required classes 'org.springframework.retry.annotation.Retryable', 'org.aspectj.lang.annotation.Aspect', 'org.springframework.boot.autoconfigure.aop.AopAutoConfiguration' (OnClassCondition)
DiscoveryClientConfigServiceBootstrapConfiguration:
Did not match:
- #ConditionalOnProperty (spring.cloud.config.discovery.enabled) did not find property 'spring.cloud.config.discovery.enabled' (OnPropertyCondition)
EncryptionBootstrapConfiguration.RsaEncryptionConfiguration:
Did not match:
- Keystore nor key found in Environment (EncryptionBootstrapConfiguration.KeyCondition)
Matched:
- #ConditionalOnClass found required class 'org.springframework.security.rsa.crypto.RsaSecretEncryptor' (OnClassCondition)
EncryptionBootstrapConfiguration.VanillaEncryptionConfiguration:
Did not match:
- #ConditionalOnMissingClass found unwanted class 'org.springframework.security.rsa.crypto.RsaSecretEncryptor' (OnClassCondition)
EurekaDiscoveryClientConfigServiceBootstrapConfiguration:
Did not match:
- #ConditionalOnProperty (spring.cloud.config.discovery.enabled) did not find property 'spring.cloud.config.discovery.enabled' (OnPropertyCondition)
Matched:
- #ConditionalOnClass found required class 'org.springframework.cloud.config.client.ConfigServicePropertySourceLocator' (OnClassCondition)
Exclusions:
None
Unconditional classes:
None
2016-11-29 00:56:37.085 DEBUG [reservation-client,,,] 28551 --- [ main] .b.l.ClasspathLoggingApplicationListener : Application started with classpath: [file:/usr/lib/jvm/java-8-oracle/jre/lib/charsets.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/deploy.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/ext/cldrdata.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/ext/dnsns.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jaccess.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jfxrt.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/ext/localedata.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/ext/nashorn.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunec.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunjce_provider.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunpkcs11.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/ext/zipfs.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/javaws.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/jce.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/jfr.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/jfxswt.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/jsse.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/management-agent.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/plugin.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/resources.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/rt.jar, file:/home/sudhir/yadavsudhir405/reservation-client/target/classes/, file:/home/sudhir/.m2/repository/org/springframework/cloud/spring-cloud-starter-stream-rabbit/1.1.1.RELEASE/spring-cloud-starter-stream-rabbit-1.1.1.RELEASE.jar, file:/home/sudhir/.m2/repository/org/springframework/cloud/spring-cloud-stream-binder-rabbit/1.1.1.RELEASE/spring-cloud-stream-binder-rabbit-1.1.1.RELEASE.jar, file:/home/sudhir/.m2/repository/org/springframework/cloud/spring-cloud-stream/1.1.0.RELEASE/spring-cloud-stream-1.1.0.RELEASE.jar, file:/home/sudhir/.m2/repository/org/springframework/boot/spring-boot-starter-validation/1.4.2.RELEASE/spring-boot-starter-validation-1.4.2.RELEASE.jar, file:/home/sudhir/.m2/repository/org/springframework/spring-messaging/4.3.4.RELEASE/spring-messaging-4.3.4.RELEASE.jar, file:/home/sudhir/.m2/repository/org/springframework/integration/spring-integration-core/4.3.5.RELEASE/spring-integration-core-4.3.5.RELEASE.jar, file:/home/sudhir/.m2/repository/org/springframework/spring-tx/4.3.4.RELEASE/spring-tx-4.3.4.RELEASE.jar, file:/home/sudhir/.m2/repository/org/springframework/integration/spring-integration-jmx/4.3.5.RELEASE/spring-integration-jmx-4.3.5.RELEASE.jar, file:/home/sudhir/.m2/repository/org/springframework/spring-tuple/1.0.0.RELEASE/spring-tuple-1.0.0.RELEASE.jar, file:/home/sudhir/.m2/repository/org/springframework/integration/spring-integration-tuple/1.0.0.RELEASE/spring-integration-tuple-1.0.0.RELEASE.jar, file:/home/sudhir/.m2/repository/org/springframework/cloud/spring-cloud-stream-codec/1.1.0.RELEASE/spring-cloud-stream-codec-1.1.0.RELEASE.jar, file:/home/sudhir/.m2/repository/com/esotericsoftware/kryo-shaded/3.0.3/kryo-shaded-3.0.3.jar, file:/home/sudhir/.m2/repository/com/esotericsoftware/minlog/1.3.0/minlog-1.3.0.jar, file:/home/sudhir/.m2/repository/org/springframework/boot/spring-boot-starter-amqp/1.4.2.RELEASE/spring-boot-starter-amqp-1.4.2.RELEASE.jar, file:/home/sudhir/.m2/repository/org/springframework/amqp/spring-rabbit/1.6.5.RELEASE/spring-rabbit-1.6.5.RELEASE.jar, file:/home/sudhir/.m2/repository/org/springframework/amqp/spring-amqp/1.6.5.RELEASE/spring-amqp-1.6.5.RELEASE.jar, file:/home/sudhir/.m2/repository/com/rabbitmq/http-client/1.0.0.RELEASE/http-client-1.0.0.RELEASE.jar, file:/home/sudhir/.m2/repository/com/rabbitmq/amqp-client/3.6.5/amqp-

Related

How do you run static content for using ( SpringBoot 2.x + weblogic + *.war ) from a classpath an application?

I have a weblogic server that I run into Inlellij Idea for audit work of an application.
I made web-app based on SpringBoot 2.x. I am using Intellij Idea 2019 and there I configure a deploy my webapplication on weblogic 12.x.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.weblogic</groupId>
<artifactId>weblogic</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>weblogic</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<version.apache.maven.plugins>3.8.1</version.apache.maven.plugins>
<version.mapstruct>1.3.0.Final</version.mapstruct>
<version.apache.common.lang3>3.9</version.apache.common.lang3>
<version.apache.commons.text>1.8</version.apache.commons.text>
<version.apache.commons.beanutils>1.9.4</version.apache.commons.beanutils>
<version.datasource.proxy>1.6</version.datasource.proxy>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</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-rest</artifactId>
</dependency>
<!--исключаем из компиляции встроенный Tomcat-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<archive>
<manifest>
<addDefaultImplementationEntries>false</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
src/main/webapp/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app>
src/main/webapp/WEB-INF/weblogic.xml
<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-web-app
http://xmlns.oracle.com/weblogic/weblogic-web-app/1.9/weblogic-web-app.xsd">
<wls:weblogic-version>12.2.1.4.0</wls:weblogic-version>
<wls:context-root>/api</wls:context-root>
<wls:container-descriptor>
<wls:prefer-application-packages>
<wls:package-name>org.slf4j.*</wls:package-name>
<wls:package-name>org.springframework.*</wls:package-name>
<wls:package-name>javax.persistence.*</wls:package-name>
</wls:prefer-application-packages>
</wls:container-descriptor>
</wls:weblogic-web-app>
src/main/webapp/index.html
When I run weblogic by Intellij Idea I get index.html by url - http://localhost:7001/api
So, It works.
But I need more complex static content and I want to place it into an application classpath.
I made so…
src/main/resources/templates/pages/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello Weblogic!!!</title>
<link type="text/css" href="/static/css/style.css" rel="stylesheet"/>
</head>
<body>
<div class="st">
Hello Weblogic!!!
</div>
</body>
</html>
src/main/resources/static/css/style.css
.st{
color: red;
font-size: 35px;
}
And then…
src/main/java/com/weblogic/weblogic/mvc/config/MyMvcConfig.java
package com.weblogic.weblogic.mvc.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
#Configuration
public class MyMvcConfig implements WebMvcConfigurer {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(
"/static/css/**",
"/templates/pages/**")
.addResourceLocations(
"classpath:/",
"classpath:/static/css/",
"classpath:/templates/pages/");
}
}
But I get … Error 500--Internal Server Error
java.lang.NullPointerException at
weblogic.servlet.internal.ServletResponseImpl.sendContentError(ServletResponseImpl.java:738)
at
weblogic.servlet.internal.ServletResponseImpl.sendError(ServletResponseImpl.java:796)
at
weblogic.servlet.internal.ServletResponseImpl.sendError(ServletResponseImpl.java:713)
at
org.springframework.boot.web.servlet.support.ErrorPageFilter$ErrorWrapperResponse.sendErrorIfNecessary(ErrorPageFilter.java:349)
at
org.springframework.boot.web.servlet.support.ErrorPageFilter$ErrorWrapperResponse.getWriter(ErrorPageFilter.java:363)
at
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$StaticView.render(ErrorMvcAutoConfiguration.java:227)
at
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1373)
at
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1118)
at
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1057)
at
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
at
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
who has any ideas how to fix this ?
You must use "template engine" – Thymeleaf. It doesn't work any other way (I would like to use pure (html + bootstrap), but it's not clear how to configure it).
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>web.spring.boot.jar</groupId>
<artifactId>web-spring-boot-jar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>web-spring-boot-jar</name>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The structure of the static resources
src/main/resources/static/css/style.css
src/main/resources/templates/index.html
src/main/resources/templates/pages/login/login.html
*.properties
src/main/resources/application.properties
These settings don't work.
spring.mvc.servlet.path=/api
server.servlet.context-path=/api
server.servlet.contextPath=/api
These settings could not be applied.
Configuration files - *.xml
src/main/webapp/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app>
If this file is missing, then the deployment occurs with errors ( in the development environment, you can see this in the console).
src/main/webapp/WEB-INF/weblogic.xml
<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app
xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
https://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd
http://xmlns.oracle.com/weblogic/weblogic-web-app
https://xmlns.oracle.com/weblogic/weblogic-web-app/1.9/weblogic-web-app.xsd">
<wls:context-root>rst</wls:context-root>
<wls:container-descriptor>
<wls:prefer-application-packages>
<wls:package-name>org.slf4j</wls:package-name>
</wls:prefer-application-packages>
</wls:container-descriptor>
</wls:weblogic-web-app>
<wls:context-root>rst</wls:context-root>
Here you need to be careful with choosing a name for the root context of the application (for example, if you chose a name for the context - /web, - > there will be an error, since this name is reserved somewhere).
Entry point
#SpringBootConfiguration
#EnableAutoConfiguration
#ComponentScan
public class WebSpringBootJarApplication extends SpringBootServletInitializer implements WebApplicationInitializer {
public static void main(String[] args) {
SpringApplication.run(WebSpringBootJarApplication.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(WebSpringBootJarApplication.class);
}
}
#SpringBootConfiguration
#EnableAutoConfiguration
#ComponentScan
You must have these 3 annotations, otherwise nothing will work.
The documentation says that #SpringBootConfiguration, replaces annotations:
#Configuration,
#EnableAutoConfiguration
#ComponentScan
#SpringBootConfiguration - indicates that this class provides an application
SpringBoot
It can also be used as an alternative to the #Configuration annotation, because the configuration can be found automatically.
#EnableAutoConfiguration - enables automatic configuration of Spring ApplicationContext by scanning class path components and registering beans that match different conditions.
That is, this annotation tries to guess and configure the beans that you may need.
Auto-configuration of classes, usually applied based on classpath
your application and on those beans that you have defined.
When using the #SpringBootConfiguration annotation, the context configuration is enabled automatically and the annotation is added, which will not have any additional effect…
But what I see here is quite different…
Without this annotation, nothing works... That is, we don't get the requested resources.
#ComponentScan - Directive configures component scanning for the classes marked with #Configuration.
Provides support in parallel using the configuration described in XML format in the element:
... But I didn't even specify the packages to scan....
And without specifying this annotation, I also can't access resources... ???
Why is that? I don't know…
What do I need to implement WebApplicationInitializer , - I don't know…
https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-weblogic
Contollers
src/main/java/web/spring/boot/jar/webspringbootjar/controller/index/IndexController.java
IndexController
#Controller
public class IndexController {
#RequestMapping(value="/", method= RequestMethod.GET)
public String index() {
return "index";
}
}
src/main/java/web/spring/boot/jar/webspringbootjar/controller/login/LoginController.java
LoginController
#Controller
public class LoginController {
#GetMapping(value = {"/log"})
public ModelAndView showUsersPage() {
return new ModelAndView("pages/login/login.html");
}
}
The creation of an artifact (Intellij Idea)
- Project Structure/Artifacts
- Alt + Insert
- Web Application: Exploded -> From module
There is the path to the directory with your project will be specified.
You can also configure
- Project Structure/Facets
And then…
Run\Edit Configuration
Here you configure - weblogic…
URL to the app(in my case)
http://localhost:7001/rst/
The same access will be available when you migrate .*war from the development environment and deploy it to an external weblogic server.
If you start an external server, you get an error
This means that you need to run maven and it must will make a *.war-file…
Also It doesn't work...
#Configuration
#EnableWebMvc
#ComponentScan(basePackageClasses = { ServletContextConfig.class })
public class ServletContextConfig implements WebMvcConfigurer {
#Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>
webServerFactoryCustomizer() {
return factory -> factory.setContextPath("/rootweb");
}
}
I suppose we only can use xml-configuration for servlet-context here.
If someone can add something, please specify what is wrong....
Can who has what ideas, about my issues which arose during this messages ....

PowerMock + Mockito mocking the constructor does not work

I am having troubles running a simple test that, accordingly with all the examples found in internet, should work without any issue.
I have a Java 8 Maven project with the following pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testingPowerMock</groupId>
<artifactId>constructorMocking</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>TestingPowerMock</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.1.0-M1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.1.0-RC1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.1.0-RC1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>1.6.6</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
As I am using JUnit5, I can run tests only from command line using the SureFire plugin with the command: mvn clean test
I created these two very simple classes:
public class Created {
private String message;
public Created(String message) {
this.message = message;
}
public String getMessage() {
return this.message;
}
}
public class Creator {
public Created create() {
return new Created("Another message");
}
}
When I run the following test, it fails.
#RunWith(PowerMockRunner.class)
#PrepareForTest(Creator.class)
public class RunnerTest {
#Test
public void test() throws Exception {
Creator creator = new Creator();
Created created = mock(Created.class);
when(created.getMessage()).thenReturn("The expected message");
whenNew(Created.class).withAnyArguments().thenReturn(created);
Created aNewlyCreatedObject = creator.create();
assertEquals("The expected message", aNewlyCreatedObject.getMessage());
}
}
It looks like the constructor call is not intercepted as Mockito is not proxying it. I tried different version of the dependencies but with no luck. I also tried changing the code using:
whenNew(Created.class).withArguments("Another message").thenReturn(created);
or
whenNew(Created.class).withArguments(anyString()).thenReturn(created);
but none of these methods have changed the outcome of the test.
Could somebody help me with this issue please?
Thank you
It turned out that downgrading to JUnit4 solved the problem.
Below the final pom.xml to use to run the test successfully. Changing the PowerMock version seems to not have any effect as I tried with 1.6.6, 1.7.1 and 2.0.0-beta.5. I will post a bug to Powermock team.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testingPowerMock</groupId>
<artifactId>constructorMocking</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>TestingPowerMock</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.20.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>1.7.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Apparently PowerMock does not support jUnit5 (yet?). Here is the epic for adding jUnit5 support. https://github.com/powermock/powermock/issues/830. It is too bad they don't have the resources to fix PowerMock because it is really a very powerful framework to make unit tests for legacy applications.
To my opinion when you need PowerMock to make a unit test, the class you try to test isn't written correctly and should be refactored to be testable. Only for legacy code this is sometimes too much to ask and therefor PowerMock gives you the ability to test an otherwise untestable class.

DataSource error with Springboot

I have an issue when running a spring boot project of mine:
I am using Vaadin for UI and Maven for dependecies. The Database is a MySQL one and I have followed the instructions from (https://spring.io/guides/gs/accessing-data-mysql/) closely.
Description:
Parameter 0 of constructor in
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
required a bean of type 'javax.sql.DataSource' that could not be
found.
- Bean method 'dataSource' not loaded because #ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
- Bean method 'dataSource' not loaded because #ConditionalOnBean (types: org.springframework.boot.jta.XADataSourceWrapper;
SearchStrategy: all) did not find any beans
Action:
Consider revisiting the conditions above or defining a bean of type
'javax.sql.DataSource' in your configuration.
With --debug:
Full auto-configuration report (too long for this post)
https://pastebin.com/DEsAkpLi
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ethereum</groupId>
<artifactId>TradeSafe</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>TradeSafe</name>
<description>TradeSafe WebService</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.web3j</groupId>
<artifactId>web3j-spring-boot-starter</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-bom</artifactId>
<version>8.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</repository>
<repository>
<id>org.jboss.repository.releases</id>
<name>JBoss Maven Release Repository</name>
<url>https://repository.jboss.org/nexus/content/repositories/releases</url>
</repository>
</repositories>
</project>
TradeLayout.java
package ethereum.tradesafe;
import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import com.vaadin.spring.annotation.SpringComponent;
import com.vaadin.ui.GridLayout;
#SpringComponent
public class TradeLayout extends GridLayout{
#Autowired
private TradeRepo repo;
#PostConstruct
void init() {
update();
}
private void setTrades(List<Trade> trades) {
removeAllComponents();
trades.forEach(trade-> addComponent(new TradeItemLayout(trade)));
}
public Object add(Trade trade) {
repo.save(trade);
update();
return null;
}
private void update() {
setTrades((List<Trade>) repo.findAll());
}
}
TradeRepo.java
package ethereum.tradesafe;
import org.springframework.data.repository.CrudRepository;
public interface TradeRepo extends CrudRepository<Trade, Long>{
}
application.properties
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://nottheproblem/db_tradesafe
spring.datasource.username=nottheproblem
spring.datasource.password=nottheproblem
Try to add #Repository annotation above the TradeRepo, and also rename it to TradeRepository
Solved this error by adding (missing in my case) maven dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

Spring Boot doesn't create automatic configuration for database from application.properties

I have followed a guide for creating simple spring boot app with JPA and when application was completed it should have created database connection based on configuration from application.properties file, however it didn't happen (I get Not an managed type exceptions). I know this is issuse with application.properties file because app runs correctly when I configure it manually like here.
Project structure:
POM.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>demo.tomek</groupId>
<artifactId>SpringMVC</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
application.properties file:
spring.datasource.url = jdbc:mysql://localhost:3306/TEST?createIfNotExists=true
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=but
spring.jpa.show-sql=true
# Enable spring data repos
spring.data.jpa.repositories.enabled=true
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=update
spring.thymeleaf.cache=false
spring.template.cache=false
static void main()
#SpringBootApplication
#ComponentScan("demo")
#EnableJpaRepositories("demo")
public class SpringBootTEST {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(SpringBootTEST.class, args);
ProductService pRepo = ctx.getBean("productService", ProductService.class);
pRepo.addSomeProducts();
CustomerService cRepo = ctx.getBean("customerService", CustomerService.class);
cRepo.addSomeCustomers();
}
}
Let me stress it again: everything works fine when I configure it manually like here: http://www.baeldung.com/2011/12/13/the-persistence-layer-with-spring-3-1-and-jpa/
I run out of ideas...
Since your entities are not in a subpackage of your application class, you also have to add the following annotation to SpringBootTEST so that Hibernate can scan your entity classes:
#EntityScan("demo.database")
Alternatively, it should also work, if you specify the package in the SpringBootApplication annotation:
#SpringBootApplication("demo")
Then you should also be able to remove the custom #ComponentScan and #EnableJpaRepositories annotation.

Spring Boot Actuator pretty print JSON

In my Spring Boot 1.3.3.RELEASE application Actuator's Health endpoint returns following JSON:
{"status":"UP","diskSpace":{"status":"UP","total":120031539200,"free":109277069312,"threshold":10485760}}
Instead of a single string I want to return pretty print JSON.
I have added following configuration into my application.yml:
spring:
jackson:
serialization:
INDENT_OUTPUT: true
but output is still returns the same single string.
How to properly configure my application in order to return pretty-print JSON ?
UPDATED
This is my application.yml
server:
port: #server.port#
contextPath: /dashboard
management:
contextPath: /actuator
spring:
jackson:
serialization:
INDENT_OUTPUT: true
jmx:
enabled: false
aop:
proxy-target-class: #spring.aop.proxy-target-class#
security:
bcryptPasswordEncoder:
strength: #spring.security.bcryptPasswordEncoder.strength#
datasource:
driverClassName: #spring.datasource.driverClassName#
url: #spring.datasource.url#
username: #spring.datasource.username#
password: #spring.datasource.password#
jpa:
database: #spring.jpa.database#
database-platform: #spring.jpa.database-platform#
hibernate.ddl-auto: #spring.jpa.hibernate.ddl-auto#
show-sql: #spring.jpa.show-sql#
ok:
client:
clientSecret: #ok.client.clientSecret#
clientPublicKey: #ok.client.clientPublicKey#
This is parent pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>example</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>
<name>Project Name</name>
<properties>
<springframework.boot.version>1.3.3.RELEASE</springframework.boot.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.source.version>1.8</java.source.version>
<java.target.version>1.8</java.target.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${springframework.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<modules>
<module>common</module>
<module>dashboard</module>
</modules>
</project>
This is common pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>example</artifactId>
<groupId>com.example</groupId>
<version>0.0.1</version>
</parent>
<artifactId>common</artifactId>
<packaging>jar</packaging>
<properties>
<spring-boot.version>1.3.1.RELEASE</spring-boot.version>
<hibernate.version>5.1.0.Final</hibernate.version>
<hibernate-jpa.version>1.0.0.Final</hibernate-jpa.version>
<hibernate-validator>5.2.4.Final</hibernate-validator>
<querydsl.version>3.7.2</querydsl.version>
<commons-lang3.version>3.4</commons-lang3.version>
<togglz.version>2.2.0.Final</togglz.version>
<UserAgentUtils.version>1.19</UserAgentUtils.version>
</properties>
<dependencies>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate-validator}</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>${hibernate-jpa.version}</version>
</dependency>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
<!-- QueryDSL -->
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>${querydsl.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>eu.bitwalker</groupId>
<artifactId>UserAgentUtils</artifactId>
<version>${UserAgentUtils.version}</version>
</dependency>
<!-- Togglz -->
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-spring-web</artifactId>
<version>${togglz.version}</version>
</dependency>
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-spring-security</artifactId>
<version>${togglz.version}</version>
</dependency>
<!-- Joda time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>${project.build.directory}/generated-resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.source.version}</source>
<target>${java.target.version}</target>
<optimize>true</optimize>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.source.version}</source>
<target>${java.target.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
This main pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>example</artifactId>
<groupId>com.example</groupId>
<version>0.0.1</version>
</parent>
<artifactId>dashboard</artifactId>
<packaging>war</packaging>
<properties>
<com.example.common.version>0.0.1</com.example.common.version>
<!-- 3rdparty -->
<primefaces.version>5.3</primefaces.version>
<primefaces-themes.version>1.0.10</primefaces-themes.version>
<jsf.version>2.2.13</jsf.version>
<togglz.version>2.2.0.Final</togglz.version>
</properties>
<repositories>
<repository>
<id>prime-repo</id>
<name>PrimeFaces Maven Repository</name>
<url>http://repository.primefaces.org</url>
<layout>default</layout>
</repository>
</repositories>
<profiles>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>common</artifactId>
<version>${com.example.common.version}</version>
</dependency>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Togglz -->
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-servlet</artifactId>
<version>${togglz.version}</version>
</dependency>
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-jsf</artifactId>
<version>${togglz.version}</version>
</dependency>
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-console</artifactId>
<version>${togglz.version}</version>
</dependency>
<!-- Apache Commons -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency>
<!-- PostgreSQL -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<!-- JSF and primefaces -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>${jsf.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>${jsf.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>${primefaces.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.primefaces.themes</groupId>
<artifactId>all-themes</artifactId>
<version>${primefaces-themes.version}</version>
</dependency>
<!-- Apache Tomcat embedded -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<scope>provided</scope>
</dependency>
<!-- Togglz -->
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-servlet</artifactId>
<version>${togglz.version}</version>
</dependency>
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-spring-web</artifactId>
<version>${togglz.version}</version>
</dependency>
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-spring-security</artifactId>
<version>${togglz.version}</version>
</dependency>
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-jsf</artifactId>
<version>${togglz.version}</version>
</dependency>
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-console</artifactId>
<version>${togglz.version}</version>
</dependency>
</dependencies>
<build>
<outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>${project.build.directory}/generated-resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.source.version}</source>
<target>${java.target.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
Chrome screenshot:
alexanoid. There are two method to pretty the output:
application.yml
spring:
jackson:
serialization:
INDENT_OUTPUT: true
or
application.properties
spring.jackson.serialization.INDENT_OUTPUT=true
Reference: https://github.com/lenicliu/eg-spring/tree/master/eg-spring-boot/eg-spring-boot-pretty-json
Both of chrome and curl are works fine.
Pls check the location of application.yml, where are you put it into?
src/main/resources/application.yml
UPDATE
#SpringBootApplication
public class Application {
public static void main(String[] args) {
System.setProperty("spring.jackson.serialization.INDENT_OUTPUT", "true");
SpringApplication.run(Application.class, args);
}
}
Despite all the great answers already mentioned, and I wish they would have worked for me, but in spring-boot-starter-1.3.3.RELEASE the only configuration that got my JSON to pretty print was Jenky's answer here: Jackson PrettyPrint for Spring 4
For convenience, I'm copying from that post the two possible solutions, XML config or Code config.
Option 1: XML Configuration
<mvc:annotation-driven>
<mvc:message-converters register-defaults="false">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="objectMapper">
<array>
<bean class="com.yourproject.example.CustomObjectMapper"/>
</array>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
Option 2: Code Configuration
#Configuration
public class CustomWebMvcConfiguration extends WebMvcConfigurationSupport {
#Override
protected void extendMessageConverters( List<HttpMessageConverter<?>> converters ) {
for ( HttpMessageConverter<?> converter : converters ) {
if ( converter instanceof MappingJackson2HttpMessageConverter ) {
MappingJackson2HttpMessageConverter jacksonConverter = (MappingJackson2HttpMessageConverter) converter;
jacksonConverter.setPrettyPrint( true );
}
}
}
}
Given that Spring Boot 2 is here for almost a year I want to add a solution for that. Yes, the question targets 1.3.3, but there already are answers for 1.5, and as the title misses the targeted version, I came across this question googling for version 2.1.1, for which we faced the same problem:
While trimmed output is fine for a machine, the screenshot you provided implies you want to read the endpoint's (health's in this case) output in a browser. The solution described at the end of this answer allows both (and more, if required) by replacing the original endpoint with a custom one that "wraps" the original one. By doing so you are allowed to return i.e. pretty printed JSON (note the produces attribute of #ReadOperation) and/or e.g. HTML styled in any way you want.
Seems Spring boot 1.5.1.RELEASE is not using MappingJackson2HttpMessageConverter
I found the way to change and supply my own ObjectMapper object to the Jacksson2JsonMessageConverter, in the same class I defined the rabbit template I added this #Bean, and all json messages are generated like JSON prettify
#Bean
public Jackson2JsonMessageConverter producerJackson2MessageConverter() {
Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
ObjectMapper jsonObjectMapper = new ObjectMapper();
jsonObjectMapper.enable(SerializationFeature.INDENT_OUTPUT);
converter.setJsonObjectMapper(jsonObjectMapper);
return converter;
}
The output returned is just json, the rendering will depend on what you a using to view the data. In chrome for example, There are plugins available to render json "pretty". I use postman for endpoint testing which does format json responses.
Try overriding the message converter bean to use a custom object mapper like so:
#Bean
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
converter.setObjectMapper(mapper);
return converter;
}