Skip to main content

Posts

Showing posts from January, 2016

SpringBoot : Internalization and i18n messages for Rest

Points To Remember Accept the Locale from Headers e.g @RequestHeader("Accept-Language") Locale locale Step 1 : Define Beans for Internationalization Add the files in resources/i18n folder as shown in image Add the following to the Application class ( The main class) @Bean public LocaleResolver localeResolver() { SessionLocaleResolver slr = new SessionLocaleResolver(); slr.setDefaultLocale(Locale.US); // Set default Locale as US return slr; } @Bean public ResourceBundleMessageSource messageSource() { ResourceBundleMessageSource source = new ResourceBundleMessageSource(); source.setBasenames("i18n/messages"); // name of the resource bundle source.setUseCodeAsDefaultMessage(true); return source; } Now inject the MessageSource Bean where you want to internationalize the message. We will be doing it in the Controller. Note : You need to define all your resouce bundles in the messageSource bean so that the application can pick them. @RestCon

SpringBoot : How to read Header from Request and bind to variable

How to read Headers in Spring Boot Controller How to Allow Custom Headers in Spring Boot  Suppose we have a controller and want to bind the Headers to a variable, then we can do the following @RequestHeader(value = "Accept-Language", defaultValue = "en") String locale This will bind the Header Accept-Language , to the variable locale. value="" It defines the name of the header to bind to the variable required= boolean It tells if this is mandatory header or not. default value is true defaultValue ="" It can be used to give a default value if the header is not present. Similarly you can bind Custom Headers like shown below // String @RequestHeader(value = "X-My-String-Header", defaultValue = "Hello World") String h1 // Long @RequestHeader(value = "X-My-Long-Header", defaultValue = 212) Long h2 Do not use " _ " underscore in header key or values, because they get neglected while htt

SpringBoot : How to bind data from url to a variable

Binding data from URL to a variable Suppose you want to have bind some data from URL then you can do the following create the @RequestMapping annotation e.g /list/{id} /{version}/list/{id} bind the Url parameters using @PathVariable annotation e.g @PathVariable("id") Long id @PathVariable("version") int version,@PathVariable("id") Long id Complete code may look like this, @RequestMapping("list/{id}") public Object list(@PathVariable("id") Long id ){ // Logic goes here } @RequestMapping("/{version}/list/{id}") public Object list(@PathVariable("version") int version,@PathVariable("id") Long id ){ // Logic goes here }

JPA : One to Many Mapping using Hibernate Spring Boot

STEP 1 :: Add the dependencies You need to add the following dependencies in your build.gradle file. compile('org.springframework.boot:spring-boot-starter-data-jpa') compile('org.springframework.boot:spring-boot-starter-jdbc') runtime('mysql:mysql-connector-java') STEP 2 :: Make Entity classes You need to create two entity QUESTION and TOPIC. Here the Topic Entity will have an annotation @OneToMany , this indicates the following This one topic can have many questions. All the questions will be deleted or updated when topic is deleted/updated. Its mapping is defined by Entity Question by the mapping of topic variable. It will give a list of all question of a topic when it is fetched If you do not need questions when topic is fetched then make fetch type as LAZY @OneToMany(cascade = CascadeType.ALL,mappedBy = "topic",fetch = FetchType.EAGER) private List questions; Here the Question entity will have the annotation @ManyToOne ,

Java : How to generate a random number between two given numbers

How to find a Random Number Between two given Numbers Suppose we want to generate a number between two given numbers X and Y then we can use the following two approaches. Approach 1 Generate a random number between 0 to (Y - X ) just add this to X. i.e X+N For, example if we have to calculate a random number between 100 and 150, then in that case X = 100 , Y = 150, Then we calculate a number between 0 to (150-100)  i.e  0 to 50. Thus, we just need to add this to 100 to get a number in range of 100 to 150. Approach 2 Generate a number N using the Math.random() method such that ( N < X< Y ) If N <= Y - X  then result is X+N else, we add the difference of N from the difference of X and Y i.e X + ( N - ( Y - X )) For, example if we have to calculate a random number between 100 and 150, then in that case X = 100 , Y = 150, Case 1 : N = 24 ( less than Y- X) In this case result will be X + N that is 24 + 100 = 124 Case 2 : N = 88 ( greater than Y-X ) In this case result will be X +