Configure gson in Spring using GsonHttpMessageConverter [deprecated]

WebMvcConfigurerAdapter is deprecated. As of Spring 5.0 do this, so this is not working any more and we should use Force Spring Boot to use Gson instead of Jackson

Excluding jackson from classpath

@SpringBootApplication
@EnableAutoConfiguration(exclude = { JacksonAutoConfiguration.class })
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Using java config

@Configuration
@EnableWebMvc
public class Application extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter < ? >> converters) {
        GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter();
        converters.add(gsonHttpMessageConverter);
    }
}

References
https://www.leveluplunch.com/java/tutorials/023-configure-integrate-gson-spring-boot/

Spring CORS No ‘Access-Control-Allow-Origin’ header is present

Enabling CORS for the whole application is as simple as:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }
}

To enable CORS for the whole controller:

@CrossOrigin(origins = "http://domain2.com", maxAge = 3600)
@RestController
@RequestMapping("/account")
public class AccountController {

    @RequestMapping("/{id}")
    public Account retrieve(@PathVariable Long id) {
        // ...
    }

    @RequestMapping(method = RequestMethod.DELETE, path = "/{id}")
    public void remove(@PathVariable Long id) {
        // ...
    }
}

To enable CORS for the method:

@RestController
@RequestMapping("/account")
public class AccountController {
  @CrossOrigin
  @RequestMapping("/{id}")
  public Account retrieve(@PathVariable Long id) {
    // ...
  }
}

References
https://stackoverflow.com/questions/35091524/spring-cors-no-access-control-allow-origin-header-is-present

How to Use CommandLineRunner in Spring Boot Application

package com.therealdanvega;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
 
@Component
public class DataLoader implements CommandLineRunner {
 
    private final Logger logger = LoggerFactory.getLogger(DataLoader.class);
 
    @Override
    public void run(String... strings) throws Exception {
        logger.info("Loading data...");
 
    }
}

References
https://www.quickprogrammingtips.com/spring-boot/how-to-use-commandlinerunner-in-spring-boot-application.html
http://therealdanvega.com/blog/2017/04/07/spring-boot-command-line-runner

@PathVariable in Spring Boot

    @RequestMapping("/news/{year}/{month}/{day}/{title}")
    public ModelAndView news(HttpServletRequest request, HttpServletResponse response,
                             @PathVariable int year, @PathVariable int month, @PathVariable int day,
                             @PathVariable String title) {

        SessionBL sessionManager = new SessionBL(request, response);

        String postUrl = String.format("/%d/%d/%d/%s", year, month, day, title);

        ModelAndView modelAndView = new ModelAndView("index");
        modelAndView.addObject("version", new Statics().getVersion());
        return modelAndView;
    }

References
http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates-regex
https://www.mkyong.com/spring-mvc/spring-rest-requestmapping-extract-incorrectly-if-value-contains/

Install Spring Boot applications

Make fully executable applications for Unix systems
A fully executable jar can be executed like any other executable binary or it can be registered with init.d or systemd
Gradle configuration:

bootJar {
  launchScript()
}

Installation as a systemd Service

create a script named myapp.service and place it in /etc/systemd/system directory

[Unit]
Description=myapp
After=syslog.target

[Service]
User=myapp
ExecStart=/var/myapp/myapp.jar
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

Remember to change the DescriptionUser, and ExecStart fields for your application.

To flag the application to start automatically on system boot, use the following command:

systemctl enable myapp.service

References
https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html
https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/sect-managing_services_with_systemd-unit_files
https://tecadmin.net/setup-autorun-python-script-using-systemd/
https://www.raspberrypi-spy.co.uk/2015/10/how-to-autorun-a-python-script-on-boot-using-systemd/