Monday, December 24, 2018

Disabling Spring Security in Spring Boot

If you have a Spring boot application configured with the Spring Security, whenever you are requesting a resource/page, it will invoke the basic security and show you a login page. So how to avoid this and access your resource without any security restriction. The simple step is use security.ignored properties. Add the below

security.ignored=/**

in your application.properties, you are good to go. Now accessing any restricted resource doesn't put any constraints for you.

Note: This works fine for version below Spring Boot 2.0.0. I haven't tried or exposed to Spring Boot 2.0.0.

Happy Programming...!!!

Saturday, June 2, 2018

Class has two properties of the same name exception

I think, it's because of the place where we use the "@XMLElement" annotation in the (bean) class. JAXB (annotation processor) considers the member field & getter method of the same field element as different properties, when we use the @XMLElement annotation at the field level it throws the IllegalAnnotationExceptions exception.

Below is the sample exception, which i encountered, when i placed the @XMLElement annotation on the property of my class

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 6 counts of IllegalAnnotationExceptions
Class has two properties of the same name "accountNum"
this problem is related to the following location:
at public java.lang.String com.test.finance.domain.ax.VendTable.getAccountNum()
at com.test.finance.domain.ax.VendTable
this problem is related to the following location:
at private java.lang.String com.test.finance.domain.ax.VendTable.accountNum
at com.test.finance.domain.ax.VendTable



To resolve this issue, JAXB offers a lot of flexibility when interacting with your object model. One area is configuring the use of fields or properties to access the data in your domain objects. This is specified as an XmlAccessType (PUBLIC_MEMBER, PROPERTY, FIELD, or NONE) via the @XmlAccessorType annotation. So, you add the XmlAccessType to your class, as shown below, will resolve the issue.

@XmlAccessorType(XmlAccessType.FIELD)


Happy Programming...!!!

Thursday, June 15, 2017

$http.get(…).success is not a function

When i was working on application using angular 1.6.4, i was using the $http to get the data from the server. So i used the below line of code

$http.get("data/places.json").success(function (results) {
$scope.places = results.data;
});

but it was throwing an error saying $http.get(…).success is not a function. When i browse through the angular documents and net, it seems to be that, the function success and error has been deprecated from 1.5, instead you have to use then method. The then() method takes two arguments: a success and an error callback which will be called with a response object.

$http.get("data/places.json").then(function (success) {
$scope.places = success.data;
},
function(error){
//do something
});


Happy Programming...!!!

Monday, June 12, 2017

JAX-RS with CXF without Spring

In this post, we will see, how to implement JAX-RS with CXF without using the Spring. Instead of using the Servlet org.apache.cxf.transport.servlet.CXFServlet we will be using the Servlet org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet. When using the CXFNonSpringJaxrsServlet, there is no need for servlet.xml, since the configuration of the endpoints and provider will be taken
automatically done, by the jaxrs.serviceClasses at runtime. Note: this will not generate the WADL file, that's the only disadvantage.

Rest of the items are similar to the normal CXF implementation.

 <web-app>  
  <display-name>cxf</display-name>  
  <servlet>  
   <servlet-name>CXFServlet</servlet-name>  
   <servlet-class>  
   org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet  
  </servlet-class>  
   <init-param>  
    <param-name>jaxrs.serviceClasses</param-name>  
    <param-value>com.school.service.StudentService</param-value>  
   </init-param>  
   <load-on-startup>1</load-on-startup>  
  </servlet>  
  <servlet-mapping>  
   <servlet-name>CXFServlet</servlet-name>  
   <url-pattern>/services/*</url-pattern>  
  </servlet-mapping>  
 </web-app>  


Happy Programming...!!

Tuesday, June 6, 2017

Invalid configuration object output.path is not an absolute path - Webpack

While i was practising the Nodejs with webpack recently i encountered an error, when i try to start the application using command npm start. I received the following error.

Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema. - configuration.output.path: The provided value "./build" is not an absolute path!

Below is the snap shot of my webpack.config.js

 
module.exports = {  
  entry: ['./app/index.js'],  
  output: {  
   path: './build',  
   filename: 'bundle.js'  
  },  
  ....
  ...
 };  


The issue is due to path provided for the output, it is a relative path. output.path requires an absolute path, we need to convert it to an absolute path. To convert it into the absolute path, we will be using the path.resolve. Add the below code to your webpack.config.js, this should resolve the issue. Now you start the server, it will work like a charm

 
const path = require('path'); //in the top<
 module.exports = {  
  entry: ['./app/index.js'],  
  output: {  
   > path: 'path.resolve(__dirname, 'build'),'<  
   filename: 'bundle.js'  
  },  
 };  

Happy Programming...!!!

Wednesday, May 24, 2017

Connecting mysql with Sequelize and Nodejs

Sequelize.js is an ORM (Object/Relational Mapper) which provides easy access to MySQL, MariaDB, SQLite or PostgreSQL databases by mapping database entries to objects and vice versa, just like how Hibernate. Sequelize is easy to learn and has dozens of cool features like synchronization, association, validation, etc. I assume you have Nodejs and MySQL installed.

Let's install Sequelize first

npm install --save sequelize

Now install the connector for the database MySQL

npm install --save mysql

That's all, now let's database connection with the below code. Save the code as mysqlConnector.js

 
var Sequelize = require('sequelize');  
 const sequelize = new Sequelize('mysql://root:password@localhost:3306/test');  
 sequelize  
  .authenticate()  
  .then(() => {  
   console.log('Connection has been established successfully.');  
  })  
  .catch(err => {  
   console.error('Unable to connect to the database:', err);  
  });  


Now execute the js, to see whether we can establish the connection with mysql. To execute the js, run the below command.

node mysqlConnector.js If we where able to establish the connection it will print 'Connection has been established successfully.'

That's all Happy Programming...!!!

Saturday, January 28, 2017

Model value ${} is not displayed in the JSP

The Spring Expression Language (SpEL) is a powerful expression language that supports querying and manipulating an object graph at runtime. It can be used with XML or annotation-based Spring configurations. Sometimes, it will make you to scratch your head, when things not working. Recently i encountered a wired problem while working on the Spring MVC project, me trying to set a value into a model, and display the value in JSP via EL, e.g ${name}, but it just outputs the result as it is – ${name}, not the “value” stored in the model.

Controller

 @Controller  
 public class HelloController {  
      @RequestMapping("/hello")  
      public String hello(Model model) {  
           model.addAttribute("name", "John Doe");  
           return "welcome";  
      }  
 }  

JSP File

 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  
 <!DOCTYPE html>  
 <html>  
 <head>  
      <title>Home Page</title>  
      <link rel='stylesheet' href='<c:url value="/resources/css/style.css" />' type='text/css' media='all' />   
 </head>  
 <body>  
      <h2>Hello World, Spring MVC</h2>  
      <p>Welcome, ${name}</p>  
      <p>Welcome, ${name}</p>  
      <p>Welcome, ${name}</p>  
 </body>  
 </html>  

Everything looks good in the above, then what makes the jsp page just to dump the EL script as it is. Actually the issue is caused by the old JSP 1.2 descriptor. If you are using the old JSP 1.2 descriptor, defined by DTD ,for example web.xml. The EL is disabled or ignored by default, you have to enable it manually, so that it will outputs the value store in the "name" model.

JSP 1.2 web.xml
 <!DOCTYPE web-app PUBLIC  
  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  
  "http://java.sun.com/dtd/web-app_2_3.dtd" >  
 <web-app>  
  <display-name>Archetype Created Web Application</display-name>  
 </web-app>  

So you just add the following tag <%@ page isELIgnored="false" %> , this will resolve the issue.

JSP 2.0
If you are using the standard JSP 2.0 descriptor, defined by w3c schema ,for example web.xml. The EL is enabled by default, and you should see the value stored in the "name" model, which is "John Doe".

 <web-app id="WebApp_ID" version="2.4"  
      xmlns="http://java.sun.com/xml/ns/j2ee"  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  
      http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
 //...  
 </web-app>  

Happy Programming...!!!