Skip to content

Feed aggregator

Java 8 Concepts: FP, Streams, and Lambda Expressions

If you’ve been raised on object-oriented programming and have used Java (or C#, C++) all your life, the sudden turn to functional programming that comes with JDK 1.8 may seem confusing and intimidating.

Are there really any benefits of this new paradigm? In which situations can you leverage the new features?

Categories: Communities

Java 9 (Part 4): Trying Try-with-Resources: First Look

This is part 4 of the java 9 series. While in part 3 we covered interfaces and the private methods that were added to them, in part 4 we see that try-with-resoures was enhanced, or we might say even fixed. Try-with-resources, which was introduced back in java 7, is a concept which helps developer close resources which are not using anymore, such as database connection, file streams, etc. Let’s see how try-with-resources behaved in java 7 and now in java 9.

Step 1: Pre-Java 7 Try-with-Resources

Try-with-Resources, prior to Java 9, meant that instead of just calling a piece of code which opens up a resource like:

Categories: Communities

JPA Auditing: Automatically Persisting Audit Logs Using EntityListeners

In my previous article, Spring Data JPA Auditing: Saving CreatedBy, CreatedDate, LastModifiedBy, LastModifiedDate automatically, I discussed why auditing is important for any business application and how we can use Spring Data JPA automate it.

I also discussed how Spring Data uses JPA's EntityListeners and callback methods to automatically update the CreatedBy, CreatedDate, LastModifiedBy, and LastModifiedDate properties.

Categories: Communities

This Week in Java Careers: Which Companies Are Hiring?

DZone Jobs launched earlier this month, and there are already some excellent opportunities for Java developers who are looking for their next career move. New jobs are being added daily, so we've started a weekly roundup to highlight these new positions. We encourage you to check these jobs out, as you may just find the perfect next career step. 

Site Reliability Engineer - Founding Team Member
Splice Machine
Location: San Francisco, CA
Experience: Demonstrated ability to write programs using a high-level programming language like: Java, Python, Ruby, Go or C++

Categories: Communities

Vaadin With Scala

This post shows a fully working 'Hello World' application in Vaadin 8 with Scala 2.12 running on Jetty.

Following my frustrated attempt to Dockerize an Ionic application as the Web UI of my petty Akka project, I started evaluating the latest version of Vaadin.

Categories: Communities

Multi-Stage Docker Image Build for Java Apps

A few days back, I discovered a new Docker feature — multi-stage builds. The multi-stage build feature helps you create thin Docker images by making it possible to divide the image building process into multiple stages. Artifacts produced in one stage can be reused by another stage. This is very beneficial for languages like Java as multiple steps are required to build the Docker image. The main advantage of the multi-stage build feature is that it can help you create smaller size images. This feature is not yet available in stable versions of Docker. It will become available in Docker 17.05. To use this feature, you have to use the edge version of Docker CE.

To build a Docker image for a Java application, you first need to build the Java project. Java build process needs a JDK and a build tool like Maven, Gradle, or Ant. Once a Java binary artifact is produced, you can package the binary in a Docker image. For running a Java binary, you only need JRE, so you don’t have to pay the cost of bundling the whole JDK.

Categories: Communities

What's New in CUBA Platform 6.5?

As you already might have noticed we have published the new release of the CUBA platform and Studio. The new version mostly introduces enhancements of the existing functionality, however, there are a few useful features have been included in 6.5.

This article covers the major enhancements of Platform and Studio. The full list of changes is available in the corresponding release notes:

Categories: Communities

jquery-stockquotes and Oracle JET

Here's https://github.com/ajwhite/jquery-stockquotes integrated into an Oracle JET application:

Steps:

  1. Somehow download https://github.com/ajwhite/jquery-stockquotes, e.g., via Bower.
  2. In "main.js" include a reference: 'jqueryui-stockquotes': 'libs/jquery-stockquotes/dist/jquery.stockquotes', 
  3. In your module, reference 'jqueryui-stockquotes' in your define block and in your "self.handleAttached" include the following:   $('.stock-quote').stockQuote();
  4. In the view, include this:     <h2>Apple:  <span class="stock-quote" data-symbol="AAPL"></span></h2>     <h2>Bank of America:  <span class="stock-quote" data-symbol="BAC"></span></h2>     <h2>Facebook:  <span class="stock-quote" data-symbol="FB"></span></h2>     <h2>IBM:  <span class="stock-quote" data-symbol="IBM"></span></h2>     <h2>Microsoft:  <span class="stock-quote" data-symbol="MSFT"></span></h2>     <h2>Oracle:  <span class="stock-quote" data-symbol="ORCL"></span></h2>     <h2>SalesForce:  <span class="stock-quote" data-symbol="CRM"></span></h2>     <h2>Twitter:  <span class="stock-quote" data-symbol="TWTR"></span></h2>
  5. In the index page, reference the CSS:
<link rel="stylesheet"   type="text/css"   href="js/libs/jquery-stockquotes/dist/jquery.stockquotes.css" />

And that's all!

Categories: Open Source

The Future Of Java

To gather insights on the state of the Java ecosystem today, we spoke to nine executives who are familiar with the ecosystem.

We asked these experienced Java professionals, "What’s the future of Java from your perspective?" Here's what they told us:

Categories: Communities

Spring Cloud – Adding Angular 4

baeldung - Coding and Testing Stuff - Fri, 05/26/2017 - 10:39
1. Overview

In our last Spring Cloud article, we added Zipkin support into our application. In this article, we’re going to be adding a front-end application to our stack.

Up until now, we’ve been working entirely on the back end to build our cloud application. But what good is a web app if there’s no UI? In this article, we’re going to solve that issue by integrating a single page application into our project.

We will be writing this app using Angular and Bootstrap. The style of Angular 4 code feels a lot like coding a Spring app which is a natural crossover for a Spring developer! While the front end code will be using Angular, the content of this article can be easily extended to any front end framework with minimal effort.

In this article, we’re going to be building an Angular 4 app and connecting it to our cloud services. We will demonstrate how to integrate login between a SPA and Spring Security.  We’ll also show how to access our application’s data using Angular’s support for HTTP communication.

2. Gateway Changes

With front end in place, we’re going to switch to form based login and secure parts of UI to privileged users. This requires making changes to our gateway security configuration.

2.1. Update HttpSecurity

First, let’s update configure(HttpSecurity http) method in our gateway SecurityConfig.java class:

@Override
protected void configure(HttpSecurity http) {
    http
      .formLogin()
      .defaultSuccessUrl("/home/index.html", true)
      .and()
    .authorizeRequests()
      .antMatchers("/book-service/**", "/rating-service/**", "/login*", "/")
      .permitAll()
      .antMatchers("/eureka/**").hasRole("ADMIN")
      .anyRequest().authenticated()
      .and()
    .logout()
      .and()
    .csrf().disable();
}

First, we add a default success URL to point to /home/index.html as this will be where our Angular app lives. Next, we configure the ant matchers to allow any request through the gateway except for the Eureka resources. This will delegate all security checks to back-end services.

Next, we removed the logout success URL, as the default redirect back to the login page will work fine.

2.2. Add a Principal Endpoint

Next, let’s add an endpoint to return the authenticated user. This will be used in our Angular app to log in and identify the roles our user has. This will help us control what actions they can do on our site.

In the gateway project, add an AuthenticationController class:

@RestController
public class AuthenticationController {
 
    @GetMapping("/me")
    public Principal getMyUser(Principal principal) {
        return principal;
    }
}

The controller returns the currently logged in user object to the caller. This gives us all the information we need to control our Angular app.

2.3. Add a Landing Page

Let’s add a very simple landing page so that users see something when they go to the root of our application.

In src/main/resources/static, let’s add an index.html file with a link to the login page:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Book Rater Landing</title>
</head>
<body>
    <h1>Book Rater</h1>
    <p>So many great things about the books</p>
    <a href="/login">Login</a>
</body>
</html>
3. Angular CLI and the Starter Project

Before starting a new Angular project make sure to install the latest versions of Node.js and npm.

3.1. Install the Angular CLI

To begin, we will need to use npm to download and install the Angular command line interface. Open a terminal and run:

npm install -g @angular/cli

This will download and install the CLI globally.

3.2. Install a new Project

While still in the terminal, navigate to the gateway project and go into the gateway/src/main folder. Create a directory called “angular” and navigate to it. From here run:

ng new ui

Be patient; the CLI’s setting up a brand new project and downloading all the JavaScript dependencies with npm. It’s not uncommon for this process to take many minutes.

The ng command’s the shortcut for the Angular CLI, the new parameter instructs that CLI to create a new project, and the ui command gives our project a name.

3.3. Run the Project

Once the new command’s complete. Navigate to the ui folder that was created and run:

ng serve

Once the project builds navigate to http://localhost:4200. We should see this in the browser:


Congratulations! We just built an Angular app!

3.4. Install Bootstrap

Let’s use npm to install bootstrap. From the ui directory run this command:

npm install bootstrap@4.0.0-alpha.6 --save

This will download bootstrap into the node_modules folder.

In the ui directory, open the .angular-cli.json file. This is the file that configures some properties about our project. Find the apps > styles property and add a file location of our Bootstrap CSS class:

"styles": [
    "styles.css",
    "../node_modules/bootstrap/dist/css/bootstrap.min.css"
],

This will instruct Angular to include Bootstrap in the compiled CSS file that’s built with the project.

3.5. Set the Build Output Directory

Next, we need to tell Angular where to put the build files so that our spring boot app can serve them. Spring Boot can serve files from two locations in the resources folder:

  • src/main/resources/static
  • src/main/resource/public

Since we’re already using the static folder to serve some resources for eureka, and Angular deletes this folder each time a build is run, let’s build our Angular app into the public folder.

Open the .angular-cli.json file again and find the apps > outDir property. Update that string:

"outDir": "../../resources/static/home",

If the Angular project’s located in src/main/angular/ui, then it will build to the src/main/resources/public folder. If the app in another folder this string will need to be modified to set the location correctly.

3.6. Automate the Build with Maven

Lastly, we will set up an automated build to run when we compile our code. This ant task will run the Angular CLI build task whenever “mvn compile” is run. Add this step to the gateway’s POM.xml to ensure that each time we compile we get the latest ui changes:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <configuration>
                <tasks>
                    <exec executable="cmd" osfamily="windows"
                      dir="${project.basedir}/src/main/angular/ui">
                        <arg value="/c"/>
                        <arg value="ng"/>
                        <arg value="build"/>
                    </exec>
                    <exec executable="/bin/sh" osfamily="mac"
                      dir="${project.basedir}/src/main/angular/ui">
                        <arg value="-c"/>
                        <arg value="ng build"/>
                    </exec>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

We should note that this set up does require that the Angular CLI be available on the classpath. Pushing this script to an environment that does not have that dependency will result in build failures.

Now let’s start building our Angular application!

4. Angular

In this section of the tutorial, we build an authentication mechanism in our page. We use basic authentication and follow a simple flow to make it work.

Users have a login form where they can enter their username and password.

Next, we use their credentials to create a base64 authentication token and request the “/me” endpoint. The endpoint returns a Principal object containing the roles of this user.

Lastly, we will store the credentials and the principal on the client to use in subsequent requests.

Let’s see how this’s done!

4.1. Template

In the gateway project, navigate to src/main/angular/ui/src/app and open the app.component.html file. This’s the first template that Angular loads and will be where our users will land after logging in.

In here, we’re going to add some code to display a navigation bar with a login form:

<nav class="navbar navbar-toggleable-md navbar-inverse fixed-top bg-inverse">
    <button class="navbar-toggler navbar-toggler-right" type="button" 
      data-toggle="collapse" data-target="#navbarCollapse" 
      aria-controls="navbarCollapse" aria-expanded="false" 
      aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
    </button>
    <a class="navbar-brand" href="#">Book Rater 
        <span *ngIf="principal.isAdmin()">Admin</span></a>
    <div class="collapse navbar-collapse" id="navbarCollapse">
    <ul class="navbar-nav mr-auto">
    </ul>
    <button *ngIf="principal.authenticated" type="button" 
      class="btn btn-link" (click)="onLogout()">Logout</button>
    </div>
</nav>

<div class="jumbotron">
    <div class="container">
        <h1>Book Rater App</h1>
        <p *ngIf="!principal.authenticated" class="lead">
        Anyone can view the books.
        </p>
        <p *ngIf="principal.authenticated && !principal.isAdmin()" class="lead">
        Users can view and create ratings</p>
        <p *ngIf="principal.isAdmin()"  class="lead">Admins can do anything!</p>
    </div>
</div>

This code sets up a navigation bar with Bootstrap classes. Embedded in the bar is an inline login form. Angular uses this markup to interact with JavaScript dynamically to render various parts of the page and control things like form submission.

Statements like (ngSubmit)=”onLogin(f)” simply indicate that when the form is submitted call the method “onLogin(f)” and pass the form to that function. Within the jumbotron div, we have paragraph tags that will display dynamically depending on the state of our principal object.

Next, let’s code up the Typescript file that will support this template.

4.2. Typescript

From the same directory open the app.component.ts file. In this file we will add all the typescript properties and methods required to make our template function:

import {Component} from "@angular/core";
import {Principal} from "./principal";
import {Response} from "@angular/http";
import {Book} from "./book";
import {HttpService} from "./http.service";

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    selectedBook: Book = null;
    principal: Principal = new Principal(false, []);
    loginFailed: boolean = false;

    constructor(private httpService: HttpService){}

    ngOnInit(): void {
        this.httpService.me()
          .subscribe((response: Response) => {
              let principalJson = response.json();
              this.principal = new Principal(principalJson.authenticated,
              principalJson.authorities);
          }, (error) => {
              console.log(error);
        });
    }

    onLogout() {
        this.httpService.logout()
          .subscribe((response: Response) => {
              if (response.status === 200) {
                  this.loginFailed = false;
                  this.principal = new Principal(false, []);
                  window.location.replace(response.url);
              }
           }, (error) => {
               console.log(error);
       });
    }
}

This class hooks into the Angular life cycle method, ngOnInit(). In this method, we call the /me endpoint to get user’s current role and state. This determine’s what the user’s sees on the main page. This method will be fired whenever this component’s created which is a great time to be checking the user’s properties for permissions in our app.

We also have an onLogout() method that logs our user out and restores the state of this page to its original settings.

There’s some magic going on here though. The httpService property that’s declared in the constructor. Angular is injecting this property into our class at runtime. Angular manages singleton instances of service classes and injects them using constructor injection, just like Spring!

Next, we need to define the HttpService class.

4.3. HttpService

In the same directory create a file named “http.service.ts”. In this file add this code to support the login and logout methods:

import {Injectable} from "@angular/core";
import {Observable} from "rxjs";
import {Response, Http, Headers, RequestOptions} from "@angular/http";
import {Book} from "./book";
import {Rating} from "./rating";

@Injectable()
export class HttpService {

    constructor(private http: Http) { }

    me(): Observable<Response> {
        return this.http.get("/me", this.makeOptions())
    }

    logout(): Observable<Response> {
        return this.http.post("/logout", '', this.makeOptions())
    }

    private makeOptions(): RequestOptions {
        let headers = new Headers({'Content-Type': 'application/json'});
        return new RequestOptions({headers: headers});
    }
}

In this class, we’re injecting another dependency using Angular’s DI construct. This time it’s the Http class. This class handles all HTTP communication and is provided to us by the framework.

These methods each perform an HTTP request using angular’s HTTP library. Each request also specifies a content type in the headers.

Now we need to do one more thing to get the HttpService registered in the dependency injection system. Open the app.module.ts file and find the providers property. Add the HttpService to that array. The result should look like this:

providers: [HttpService],
4.4. Add Principal

Next, let’s add our Principal DTO object in our Typescript code. In the same directory add a file called “principal.ts” and add this code:

export class Principal {
    public authenticated: boolean;
    public authorities: Authority[] = [];
    public credentials: any;

    constructor(authenticated: boolean, authorities: any[], credentials: any) {
        this.authenticated = authenticated;
        authorities.map(
          auth => this.authorities.push(new Authority(auth.authority)))
        this.credentials = credentials;
  }

    isAdmin() {
        return this.authorities.some(
          (auth: Authority) => auth.authority.indexOf('ADMIN') > -1)
    }
}

export class Authority {
    public authority: String;

    constructor(authority: String) {
        this.authority = authority;
    }
}

We added the Principal class and an Authority class. These are two DTO classes, much like POJOs in a Spring app. Because of that, we do not need to register these classes with the DI system in angular.

Next, let’s configure a redirect rule to redirect unknown requests to the root of our application.

4.5. 404 Handling

Let’s navigate back into the Java code for the gateway service. In the where GatewayApplication class resides add a new class called ErrorPageConfig:

@Component
public class ErrorPageConfig implements ErrorPageRegistrar {
 
    @Override
    public void registerErrorPages(ErrorPageRegistry registry) {
        registry.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND,
          "/home/index.html"));
    }

}

This class will identify any 404 response and redirect the user to “/home/index.html”. In a single page app, this’s how we handle all traffic not going to a dedicated resource since the client should be handling all navigable routes.

Now we’re ready to fire this app up and see what we built!

4.6. Build and View

Now run “mvn compile” from the gateway folder. This will compile our java source and build the Angular app to the public folder. Let’s start the other cloud applications: config, discovery, and zipkin. Then run the gateway project. When the service starts, navigate to http://localhost:8080 to see our app. We should see something like this:

Next, let’s follow the link to the login page:

Log in using the user/password credentials. Click “Login”, and we should be redirected to /home/index.html where our single page app loads.

It looks like our jumbotron is indicating we’re logged in as a user! Now log out by clicking the link the upper right corner and log in using the admin/admin credentials this time.

Looks good! Now we’re logged in as an admin.

5. Conclusion

In this article, we have seen how easy it’s to integrate a single page app into our cloud system. We took a modern framework and integrated a working security configuration into our application.

Using these examples, try to write some code to make a call to the book-service or rating-service. Since we now have examples of making HTTP calls and wiring data to the templates, this should be relatively easy.

If you would like to see how the rest of the site’s built as always, you can find the source code over on Github.

Categories: Blogs

Java Profilers: 3 Different Types and Why You Need All of Them

Debugging performance issues in production can be a pain and in some cases impossible without the right tools. Java profilers have been around forever, but the profilers most developers think about are only one type.

Let’s dive into the three different kinds of Java profilers:

Categories: Communities

10 Nice Examples of Writing SQL in Kotlin With jOOQ

Kotlin is the next big thing. With Google announcing official support for Kotlin on Android, we’ll see a lot more traction for this lovely language.

We’ve already blogged about the Kotlin language recently: 10 Features I Wish Java Would Steal From the Kotlin Language.

Categories: Communities

Java SE 9: What's New?

Below, I have provided some of the most important core language enhancements for JDK 9.0.The objective of this article is to introduce you to the new features of Java SE 9. This includes mostly conceptual introduction of the features. These are the almost finalized features, that have been accepted and officially announced by Oracle. Java 9 is scheduled for release by about the end of July 2017.

 

Categories: Communities

Atlassian Clover Is Now Open Source

Our company mission – to unleash the potential in every team – shapes the products we create today. A big part of this is delivering new features and improvements through continual updates and releases.

For many years, Atlassian Clover has provided Java and Groovy developers a reliable source for code coverage analysis. This dependability has allowed us to focus our development efforts on delivering new features and improvements to our core offerings, including JIRA Software, Bitbucket, and others.

Categories: Communities

Java Web Weekly, Issue 178

baeldung - Coding and Testing Stuff - Thu, 05/25/2017 - 12:05

Lots of interesting writeups on Java 9 this week.

Here we go…

1. Spring and Java >> Under The Hood With the JVM’s Automatic Resource Management [infoq.com]

A deep dive into the mechanics behind finalize() and try-with-resources.

>> Hibernate with PostgreSQL – 6 things you need to know [thoughts-on-java.org]

If you’re working with Hibernate and PostgreSQL – definitely some good points to consider.

>> Most popular Java application servers: 2017 edition [plumbr.eu]

An interesting insight into the market share of Java application servers. Looks like Tomcat is dominating.

Also worth reading:

Webinars and presentations:

Time to upgrade:

2. Technical >> On supporting Continuous Testing with FITR test automation [ontestautomation.com]

FITR is like ACID for testing – definitely worth having a look.

>> Enough with the microservices [aadrake.com]

The complexity added by using microservices in your architecture is non-trivial, so it’s well worth understanding the tradeoffs and the scenarios where this solution doesn’t make sense.

Also worth reading:

3. Musings >> The Relationship Between Team Size and Code Quality [daedtech.com]

The increase in the team size does not always result in the increase of the code quality – definitely keep that in mind.

Also worth reading: 4. Comics

And my favorite Dilberts of the week:

>> You lost our data center? [dilbert.com] >> Come up with a good name? [dilbert.com] >> Planned power outage [dilbert.com] 5. Pick of the Week

The last day (Friday) of the Certification Class launch:

>> The new “Certification Class” in REST With Spring >> The new “Certification Class” in Learn Spring Security
Categories: Blogs

An Overview of CDI Events

Java Enterprise Edition has many features that really stand out. One of the best is the event mechanism, which is part of the Contexts & Dependency Injection for Java specification. Events have been present in Java EE for a long time. The design of the events mechanism is extremely clean, and learning how to use events is therefore very simple. This overview is aimed at developers who are not familiar with the event mechanism and want to get to know the basics. Advanced features of CDI 2.0, like Asynchronous events, are not covered. You will learn to:

  • Fire specific events.
  • Use event qualifier.
  • Observe events fired during transactions.
  • Configure even observer bean instantiation.

At the end of this article, you will find instructions for a quick-to-run sample application available on GitHub, demonstrating capabilities of CDI events.

Categories: Communities

This Week in Spring: Manifest Editing, Boot 2.0M1, and Kotlin

Welcome to another installment of This Week in Spring! This week I’m in Amsterdam, NL and Essen, DE, talking to customers about cloud-native transformation. We’ve got a lot to cover so let’s get to it.

Categories: Communities

Notes on Debugging Clojure Code

Clojure is a great programming language, but a recurring complaint I keep hearing from developers hacking on Clojure code is that debugging can be unpleasant. First of all, I agree! Debugging Clojure code can be more daunting on average than, say, debugging Python code. This is mainly due to two reasons:

  1. Clojure's Java legacy. Clojure is compiled to Java bytecode, which has some terminology and idiosyncracies Clojure programmers aren't always familiar with. These terms tend to pop up in stack traces and cause confusion (e.g. IFN).
  2. Clojure — being a Lisp — has a certain code structure which is different from, say, a more common imperative coding style. Rather than being a sequence of statements, Clojure programs tend to involve long call chains of nested expressions. Where only part of an expression fails, it's often non-trivial to figure out why.

In this post, I want to share some notes from my own experience debugging Clojure programs.

Categories: Communities

The Strange Relationship Between Duplication and Coupling

This short post hopefully contains no new knowledge for you. Its mere intention is to make you contemplate an interesting relationship between coupling and duplication for a while.

Duplication Is Bad!

As professional programmers, true software craftsmen, the clean coders, [insert your title here], we’re often taught that duplication is bad and should be avoided at all cost. We even have (at least) two widely known principles related to this problem, namely, "Don’t Repeat Yourself" and "Once And Only Once."

Categories: Communities

Spring Data JPA Auditing: Automatically Saving the Good Stuff

In any business application, auditing simply means tracking and logging every change we do in our persisted records, which simply means tracking every insert, update, and delete operation and storing it.

Auditing helps us in maintaining history records, which can later help us in tracking user activities. If implemented properly, auditing can also provide us similar functionality to version control systems.

Categories: Communities