Category Archives: Spring

Convert a object into JSON in REST service by Spring MVC

Questions: I’m trying to create a REST service using Spring MVC and it’s working if I’m returning a plain string. My requirement is to return a JSON string of the Java object. Don’t know how to achieve this by implicit conversion. Here is my code: package com.spring.schoolmanagement.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import… Read More »

How to ask RabbitMQ to retry when business Exception occurs in Spring Asynchronous MessageListener use case

Questions: I have a Spring AMQP message listener running. public class ConsumerService implements MessageListener { @Autowired RabbitTemplate rabbitTemplate; @Override public void onMessage(Message message) { try { testService.process(message); //This process method can throw Business Exception } catch (BusinessException e) { //Here we can just log the exception. How the retry attempt is made? } catch (Exception… Read More »

What is the default request method type for the request mapping?

Questions: What is the default request method type for the @RequestMapping? @RequestMapping(value = “addGoal”) public String addGoal(Model model) {…} Answers: If you do not specify any mapping this method will resolve all the http request i.e. you can send GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE request to the specified url and it will… Read More »

Controlling logback environment dependent properties in external application properties file

Questions: In my spring-boot application I use logback for logging. The application uses external file for setting environment specific application properties and starts with option: -spring.config.location=path/to/file. I would like logback configuration to read properties from there as well so that all environment properties are managed in one place. I tried the approach described here,… Read More »

Spring context tests can't find config locations

Questions: I have a large application spread across multiple Spring bean definition xml files. In my test suite I manually load up the XML files I need using a FileSystemXmlApplicationContext to perform the tests I want to run. This reduces test set up time and allows me to use the same exact configuration files that… Read More »

ApplicationContext.getBean(Class clazz) doesn't go well with proxies

Questions: I have a bean definition in Spring and it’s proxy counterpart which is meant to be used everywhere: <bean name=”my.Bean” class=”org.springframework.aop.framework.ProxyFactoryBean” scope=”prototype”> <property name=”proxyInterfaces” value=”my.Interface”/> <property name=”target” ref=”my.BeanTarget”/> <property name=”interceptorNames”> <list> <value>someInterceptor</value> </list> </property> </bean> <bean name=”my.BeanTarget” class=”my.InterfaceImpl” scope=”prototype”> <property name=”foo” ref=”bar”/> </bean> This all works well; and in pre-Spring v3 world I was… Read More »

injecting mock beans into spring context for testing

Questions: I know similar questions have been asked, e.g. here, but having done a search, I’ve come upon a solution I’m much happier with here My only problem however, is that I’m not sure how to implement this solution. What I want to be able to do is via the HotswappableTargetSource override the bean definitions… Read More »