Monday, 24 October 2016

Disable start up of Hibernate configuration in Spring boot.

#Exclude start up of Hibernate JPA configuration

Add below entry in your application.properties file


spring.autoconfigure.exclude[0]=org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration

Friday, 14 October 2016

Error: [$http:badreq] Http request configuration url must be a string. Received


$http.get is a shortcut method for $http({ method: 'GET' }), and expects the URL as the first parameter.


Solution:
 
$http({
  method: 'JSONP',
  url: url
}).then(function successCallback(response) {
  // ok
}, function errorCallback(response) {
  // ko
});



Highlight code snippet in Blogger post.

I m using this pretty tool for my blog to highlight the codes.

Its colorful and also provide formatting of codes.


Go to link :
http://hilite.me/


So will see below window 




1. Just copy paste your code in the source code window.
2. select your language.
3. select your style.
4. Choose line numbers if you want to show.
5. click on highlight button.

Preview window will open with colorful text and formatted lines.


This is easy to use and pretty cool.

Thursday, 13 October 2016

exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.json.JSONObject

Could not write content: No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )


controller methods return simple POJOs - Collection<Bookmark>, and Bookmark, etc., 

When an HTTP request comes in that specifies an Accept header, Spring MVC loops through the configured HttpMessageConverter until it finds one that can convert from the POJO domain model types into the content-type specified in the Accept header, if so configured.

Spring Boot automatically wires up an HttpMessageConverter that can convert generic Object's to JSON, absent any more specific converter. HttpMessageConverter s work in both directions: incoming requests bodies are converted to Java objects, and Java objects are converted into HTTP response bodies.

To solve Exception just override the default behaivour of the HttpMessageConverter provided by spring.

    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
         MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
                ObjectMapper objectMapper = new ObjectMapper();
         objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
         objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
         jsonConverter.setObjectMapper(objectMapper);
         return jsonConverter;
    }


This will do the trick for you.

Caused by: java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required


A Simple DAO class extends JdbcDaoSupport, but, unable to inject or @autowired a “dataSource”, the method setDataSource is final, can’t override.

Solution

To quickly fix it, uses @PostConstruct to inject the dataSource like this :


@Repository
public class UserDetailsDaoImpl extends JdbcDaoSupport implements UserDetailsDao {

 @Autowired
 private DataSource dataSource;

 @PostConstruct
 private void initialize() {
  setDataSource(dataSource);
 }

}


Alternatively, create an own implementation of JdbcDaoSupport class, and do whatever you want. Dive inside the source code of JdbcDaoSupport, it’s just a simple helper class to create a jdbcTemplate.


Source :- https://www.mkyong.com/spring/how-to-autowire-datasource-in-jdbcdaosupport/

org.hibernate.hql.internal.ast.querysyntaxexception: "table_name" is not mapped Spring boot

Put your SampleWebJspApplication.java in pl.test package and execute the application.

It is recommended by Spring Boot to run your application from the root package and all your entities, controllers,DAO's and other service classes should be placed in child packages. This is not a hard and fast rule but it ensures all your subpackage annotated classes are scanned properly

In your case your User class is not scanned and Hibernate is not able to find the mapping for the same and eventually throwing the QuerySyntaxException when executing the from User query.

Currently Spring Boot will scan @Entity classes if they are placed in the same package or sub-packages where you specified the @SpringBootApplication annotation. Atleast, this is what I observed while developing spring boot apps lately.

If you do not want to move the location of the 

SampleApplication.java, then use 

@EntityScan(basePackages = "pl.test.model")

 annotation and this will fix the issue.

Wednesday, 12 October 2016

spring boot controllers not initialized

Controllers are not initialize because you have not scan the packages for controller

add these lines in your main class.

If you are scanning a particular class for controller.

@SpringBootApplication
@ComponentScan(basePackageClasses = temInventoryController.class)
public class InventoryApp {


if you want to scan a whole package for controller add this.

@ComponentScan(basePackages = "com.home.controller")





ORACLE : Io exception: The Network Adapter could not establish the connection

Not connecting to correct Database in oracle :


jdbc:oracle:thin:@<server_host>:1521:<instance_name>

the following commands will help:

1. Oracle query command to check the SID (or instance name):

select sys_context('userenv','instance_name') from dual; 


2. Oracle query command to check database name (or server host):

select sys_context('userenv', 'server_host') from dual;

Tuesday, 11 October 2016

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project mrpapp: Compilation failure

[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?


 Go to Window → Preferences → Java → Installed JREs.

And see if there is an entry pointing to your JDK path, and if not, click on Edit button and put the path you configured your JAVA_HOME environment


Spring boot with CORS

CORS (Cross-Origin Resource Sharing) errors occur when a web application running in a browser requests a resource from a different domain or...