xmlns:th="http://www.thymeleaf.org" not recognized in html not working - html

im passing an attribute to html page but it doesn't display i am using spring
this is my controller function
#RequestMapping(value = "/login", method = RequestMethod.POST )
public RedirectView loginCustomer(HttpServletRequest request, RedirectAttributes redir){
String username = request.getParameter("username");
String password = request.getParameter("password");
Customer customer = new Customer(username, password);
RedirectView redirectView = new RedirectView();
redirectView = CustomerService.checkCredintials(customer);
redir.addFlashAttribute("customers",username);
return redirectView;
}
and this is my customerservice
public static RedirectView checkCredintials(Customer customer) {
Optional<Customer> customerOptional = customerRepository.findCustomerByUsername(customer.getUser_name());
Optional<Customer> customerOptional2 = customerRepository.findCustomerByPassword(customer.getPassword());
RedirectView redirectView = new RedirectView();
if(customerOptional.isPresent() && customerOptional2.isPresent())
{
redirectView.setUrl("/userdashboard.html");
redirectView.setContextRelative(true);
}
else {
redirectView.setUrl("/login.html");
redirectView.setContextRelative(true);
}
return redirectView;
}
and this is my html page
<html xmlns:th="http://www.thymeleaf.org">
<body>
<h1 >${customers}</h1>
</body>
</html>
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.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>CarRentalSystem</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>CarRentalSystem</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.3.3.RELEASE</version>
</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-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
just trying to check

I found out that i was using IntelliJ community edition which not support web development i downloaded ultimate edition and everything worked

Related

How to create swagger documentation to list of object with Kotlin

I'm trying to use springdoc to generate the documentation and would like to know how can I make a nested list of object to appear in the documentation.
I created an github example to reproduce the problem:
https://github.com/marciovmartins/springdoc-nested-list-object
Here is my spring boot application, repository and entities
package example.springdoc
import com.fasterxml.jackson.annotation.JsonIgnore
import java.time.LocalDate
import java.util.UUID
import javax.persistence.CascadeType
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
import javax.persistence.JoinColumn
import javax.persistence.OneToMany
import javax.validation.Valid
import javax.validation.constraints.NotEmpty
import javax.validation.constraints.NotNull
import javax.validation.constraints.PastOrPresent
import org.hibernate.annotations.Type
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.data.repository.CrudRepository
#SpringBootApplication
class SpringDocApplication
fun main(args: Array<String>) {
runApplication<SpringDocApplication>(*args)
}
#Suppress("unused")
interface GameDayRepository : CrudRepository<GameDay, UUID>
#Suppress("unused")
#Entity(name = "gameDays")
class GameDay(
#Id
#Suppress("unused")
#Type(type = "uuid-char")
#Column(length = 36, unique = true, nullable = false)
var id: UUID? = null,
#field:NotNull
#field:PastOrPresent
var date: LocalDate,
#field:Valid
#field:NotEmpty
#JoinColumn(name = "game_day_id", nullable = false)
#OneToMany(cascade = [CascadeType.ALL], orphanRemoval = true)
var matches: Set<Match>,
)
#Suppress("unused")
#Entity(name = "matches")
class Match(
#Id
#JsonIgnore
#GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long? = null,
var goalsTeamA: Long,
var goalsTeamB: Long,
)
Here is the 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.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>SpringDoc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringDoc</name>
<description>SpringDoc</description>
<properties>
<java.version>17</java.version>
<kotlin.version>1.6.10</kotlin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</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.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-data-rest</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-kotlin</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
<plugin>jpa</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-noarg</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
And here is the screenshot of the running documentation:

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>

How to force hibernate to use proper database? Spring Boot

I cannot force hibernate to use proper database.
Consider we have two databases with same table names on one MySQL instance, and spring boot config:
spring.datasource.url=jdbc:mysql://localhost:3306/old_indeeddb?serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.default_schema=old_indeeddb
spring.jpa.hibernate.ddl-auto=validate
But when I run application it connects to wrong database indeeddb,instead of old_indeeddb . Here from log:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Missing column: scraper_task_code in indeeddb.job_chron_stg
As you see I tried solution flagged as it works:
Change database schema used by Spring Boot
but it fails for me.
Not know why. Here my pom:
<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>healthjobs</groupId>
<artifactId>scraper</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>HealthJobsScraper</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.6.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<!-- <version>1.3.3.RELEASE</version> can safely omit -->
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.3</version>
</dependency>
<!--
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.4</version>
</dependency>
-->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<!--
<version>9.4.1208</version>
<version>9.4.1207.jre7</version>
-->
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<!--
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.11.0</version>
</dependency>
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-cypher-compiler-2.2</artifactId>
<version>2.2.5</version>
</dependency>
</dependencies>
<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>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>

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;
}