Spring mvc messagesource failed in annotation - spring

I am able to retrieve message with this code
public class UserApp {
public static void main(String[] args) {
ApplicationContext appContext
= new ClassPathXmlApplicationContext("application-configuration.xml");
System.out.println(appContext.getMessage("NotEmpty.userForm.name",
new Object[] { 28,"http://www.mkyong.com" }, Locale.US ));
}
}
But failed with the same application-configuration.xml using ContextConfiguration Annotation. it is showing me
Caused by: org.springframework.context.NoSuchMessageException: No message found under code 'NotEmpty.userForm.name' for locale 'en_US'.
#Controller
#ContextConfiguration("/application-configuration.xml")
public class UserController {
private static final Logger logger =
LoggerFactory.getLogger(UserController.class);
#Autowired
ApplicationContext appContext;
#RequestMapping(value = "/users/add", method = RequestMethod.GET)
public String showAddUserForm(Model model) {
String temp = appContext.getMessage("NotEmpty.userForm.name",
new Object[] { 28,"http://www.mkyong.com" }, Locale.US ));
}
}
application-configuration.xml
<beans:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<beans:property name="basename">
<beans:value>locale/messages</beans:value>
</beans:property>
</beans:bean>
Did i use the wrong annotation for this?

Related

How to create multiple instances of RestController in Spring boot application?

I want to create different instances of Spring RestController with different instances of beans (service, dao, cache) injected.
Currently, we have implemented this with JAX-RS Restful API.
But when I try to implement the same in Spring RestController, I'm getting below error when Spring tries to create a new bean "product2RestController".
Because bean "product1RestController" is already mapped to the rest url and created.
Stacktrace:
Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'product2RestController' method
#Path("/products")
public class ProductLookupRestService {
#Context
HttpServletRequest httpServletRequest;
#Context
HttpServletResponse httpServletResponse;
#Context
UriInfo uriInfo;
private Map<Integer, AbstractProductRestService> productServiceLoopup;
public void setProductServiceLoopup(Map<Integer, AbstractProductRestService> productServiceLoopup) {
this.productServiceLoopup = productServiceLoopup;
}
#Path("/{productId}")
public AbstractProductRestService getReport(#PathParam("productId") int productId) {
AbstractProductRestService productRestService = this.productServiceLoopup.get(productId);
productRestService.setHttpServletRequest(httpServletRequest);
productRestService.setHttpServletResponse(httpServletResponse);
productRestService.setUriInfo(uriInfo);
return productRestService;
}
}
public abstract class AbstractProductRestService {
#Context
protected HttpServletRequest httpServletRequest;
#Context
protected HttpServletResponse httpServletResponse;
#Context
protected UriInfo uriInfo;
protected IProductService productService;
protected IProductCache productCache;
public void setHttpServletRequest(HttpServletRequest httpServletRequest) {
this.httpServletRequest = httpServletRequest;
}
public void setHttpServletResponse(HttpServletResponse httpServletResponse) {
this.httpServletResponse = httpServletResponse;
}
public void setUriInfo(UriInfo uriInfo) {
this.uriInfo = uriInfo;
}
public void setProductService(IProductService productService) {
this.productService = productService;
}
public void setProductCache(IProductCache productCache) {
this.productCache = productCache;
}
#POST
#Path("/filters")
#Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response createFilters(Form filterForm) {
//Save the filters and return uuid
ResponseBuilder responseBuilder = Response.created(uriInfo.getAbsolutePathBuilder().path("uuid").build());
return responseBuilder.build();
}
}
public class ProductRestService extends AbstractProductRestService {
#GET
#Path("/filters/{uuid}/detail")
public String getProductDetail(){
// do cache check : productCache
return this.productService.readProductDetail();
}
}
public class ProductServiceImpl implements IProductService{
#Override
public String readProductDetail() {
// Call to dao to get the data
return null;
}
}
<bean id="product1RestService" class="com.poc.jaxrs.rest.ProductRestService">
<property name="productService" ref="product1Service"/>
<property name="productCache" ref="product1Cache"/>
</bean>
<bean id="product2RestService" class="com.poc.jaxrs.rest.ProductRestService">
<property name="productService" ref="product2Service"/>
<property name="productCache" ref="product2Cache"/>
</bean>
<bean id="restProductLookupService" class="com.poc.jaxrs.rest.ProductLookupRestService">
<property name="productServiceLoopup">
<map key-type="java.lang.Integer">
<entry key="1"><ref bean="product1RestService"/></entry>
<entry key="2"><ref bean="product2RestService"/></entry>
</map>
</property>
</bean>
I use below like urls to invoke service.
POST Call: http://localhost:8080/productservice/products/1/filters
GET Call: http://localhost:8080/productservice/products/1/filters/uuid/detail
In my spring application context xml, I'm creating multiple instances of ProductRestService class with different beans (service, cache) instances injected.
Same way I want to create multiple instances of Spring RestController, But I'm getting Ambiguous mapping error.
Thanks for your help in advance.

How to inject bean in a Tapestry service

I want to inject bean in a Tapestry service (not in a page).
For the moment, I use this :
public class EntityRealm extends AuthorizingRealm {
ApplicationContext ctx = new ClassPathXmlApplicationContext("/application-context-security.xml");
SecurityServices securityServices = (SecurityServices)ctx.getBean("securityServices");
It works, but I want use this :
public class EntityRealm extends AuthorizingRealm {
#Inject
private SecurityServices securityServices;
And my applicationContext is in the web.xml.
In this second case, the injection doesn't work. Why ?
AppModule.java :
public class AppModule
{
//#Resource(name = "realm")
#Inject
private static EntityRealm realm;
#Contribute(WebSecurityManager.class)
public static void addRealms(Configuration<EntityRealm> configuration) {
//EntityRealm realm = new EntityRealm();
configuration.add(realm);
}
public static void contributeFactoryDefaults( MappedConfiguration<String, Object> configuration)
{
configuration.override(SecuritySymbols.LOGIN_URL, "/login");
configuration.override(SecuritySymbols.UNAUTHORIZED_URL, "/login");
configuration.override(SecuritySymbols.SUCCESS_URL, "/index");
configuration.override(SymbolConstants.APPLICATION_VERSION, "2.0-SNAPSHOT");
}
public static void contributeApplicationDefaults(MappedConfiguration<String, Object> configuration)
{
configuration.add(SymbolConstants.HMAC_PASSPHRASE, new BigInteger(130, new SecureRandom()).toString(32));
configuration.add(SymbolConstants.SUPPORTED_LOCALES, "en,fr");
configuration.add( "tapestry.default-cookie-max-age", "31536000" );
}
public RequestFilter buildTimingFilter(final Logger log)
{
return new RequestFilter()
{
public boolean service(Request request, Response response, RequestHandler handler)
throws IOException
{
long startTime = System.currentTimeMillis();
try
{
return handler.service(request, response);
} finally
{
long elapsed = System.currentTimeMillis() - startTime;
log.info(String.format("Request time: %d ms", elapsed));
}
}
};
}
public void contributeRequestHandler(OrderedConfiguration<RequestFilter> configuration,
#Local
RequestFilter filter)
{
configuration.add("Timing", filter);
}
}
And the EntityRealm.java :
public class EntityRealm extends AuthorizingRealm {
//***************************************
//************* Attributes *************
//***************************************
//ApplicationContext ctx = new ClassPathXmlApplicationContext("/application-context-security.xml");
//SecurityServices securityServices = (SecurityServices)ctx.getBean("securityServices");
//#Resource(name = "securityServices")
#Inject
private SecurityServices securityServices;
//***************************************
//************ Constructors *************
//***************************************
public EntityRealm() {
super(new MemoryConstrainedCacheManager());
setName("myapprealm");
setAuthenticationTokenClass(UsernamePasswordToken.class);
}
//***************************************
//********** Public Methods *************
//***************************************
#Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
if (principals == null) throw new AuthorizationException("PrincipalCollection was null, which should not happen");
application-context.xml :
<bean id="realm" class="net.atos.m2m.telecom.ihm.services.EntityRealm">
<property name="securityServices" ref="securityServices"></property>
</bean>
<bean id="securityServices" class="net.atos.m2m.telecom.ihm.applicatif.services.security.impl.SecurityServicesImpl">
<property name="servicesTelSecu" ref="servicesTelSecu"></property>
<property name="converterSecDSPtoDTO" ref="converterSecDSPtoDTO"></property>
<property name="converterSecDTOtoDSP" ref="converterSecDTOtoDSP"></property>
</bean>
Can you help me ?
Thank you.
How i say in previous comment, if you create EntityRealm in this way .. new EntityRealm() the inject\autowire does not work.
You must define EntityRealm as bean .. XML or Annotation.
<bean id="entityRealm" class="package.EntityRealm"/>
<bean id="securityServices" class="package.SecurityServices"/>
You can use #Resource instead,
#Resource(name = "securityServices")
private SecurityServices securityServices;
And make sure that application-context-security.xml file is loaded by Spring.

Spring doesn't use my Conerverter. Why?

I have custom Converter:
public class DateTimeConverter implements Converter<String, DateTime> {
private static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";
private DateTimeFormatter formatter;
private String datePattern = DEFAULT_DATE_PATTERN;
public String getDatePattern() {
return datePattern;
}
#Autowired(required = false)
public void setDatePattern(String datePattern) {
this.datePattern = datePattern;
}
#PostConstruct
public void init() {
formatter = DateTimeFormat.forPattern(datePattern);
}
#Override
public DateTime convert(String source) {
if (source == null) return new DateTime();
return formatter.parseDateTime(source);
}
}
And field in JavaBean:
#NotNull
#Column(name = "dateandtime")
private DateTime dateAndTime;
I registered my converter in setting:
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.myapp.util.DateTimeConverter"/>
</list>
</property>
</bean>
I got this exception:
Failed to convert property value of type 'java.lang.String' to required type 'org.joda.time.DateTime' for property 'dateAndTime'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.joda.time.DateTime] for property 'dateAndTime': no matching editors or conversion strategy found
Test:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration("file:src/main/resources/spring/business-config.xml")
public class JdbcTransactionRepositoryImplTest extends TestCase {
private static final Logger logger = Logger.getLogger(JdbcTransactionRepositoryImplTest.class);
#Autowired
private ApplicationContext context;
private JdbcTransactionRepositoryImpl transactionRepository;
#Before
public void setup() {
transactionRepository = new JdbcTransactionRepositoryImpl((DataSource) context.getBean("dataSource"));
}
#Test
public void testFindById() throws Exception {
Transaction tr1 = transactionRepository.findById(1);
assertEquals(new Long(1L), tr1.getId());
}
But, in this case:
#Test
public void testFindById() throws Exception {
ConversionService conversionService = (ConversionService) context.getBean("conversionService");
assertTrue(conversionService.canConvert(String.class, DateTime.class));
Build success!
I don't understand: why?
Thank you for any help
You don't have to create your own converter or register conversion service - if Joda-Time is in project's classpath, Spring will automagically enable conversions through #DateTimeFormat annotation (<mvc:annotation-driven /> is required).
So what you need is just:
#NotNull
#DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private DateTime dateAndTime;

Getting jdbcTemplate null in Spring Junit test case

I am writing junit test case for spring 3 restful services. When I am trying to execute it as junit, i am getting JdbcTemplate as null. I am not sure where I did the mistake. Please help me to get out of this...
LoginServiceImpl.java file,
private NamedParameterJdbcTemplate jdbcTemplate;
#Autowired
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
System.out.println("--------------"+jdbcTemplate.toString());
}
private static Map<String, AuthToken> tokenHash = new ConcurrentHashMap<String, AuthToken>();
private static String authTokenDetailsSql = "select * from authtoken where token = :token";
#Override
#RequestMapping(value = "/register", method = RequestMethod.POST)
#ResponseBody
public ServiceBean newAccount(#RequestBody Registration registration) {
String newAccountSql = "INSERT INTO account (email,password,name) VALUES (:email,:password,:name)";
ServiceDataBean<AuthToken> retBean = new ServiceDataBean<AuthToken>();
try {
System.out.println("register service calling.....");
MapSqlParameterSource namedParameters = new MapSqlParameterSource();
namedParameters.addValue("email", registration.getEmail());
messageDigest = MessageDigest.getInstance("MD5");
byte[] md5 = new byte[64];
messageDigest.update(registration.getPassword().getBytes("iso-8859-1"), 0, registration.getPassword().length());
md5 = messageDigest.digest();
namedParameters.addValue("password", convertedToHex(md5));
namedParameters.addValue("name", registration.getName());
GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
// TODO what to do with the updInt also check it's not -1
int updInt = jdbcTemplate.update(newAccountSql, namedParameters, generatedKeyHolder);
long accountId = (Long) generatedKeyHolder.getKeys().get("GENERATED_KEY");
registration.getDevice().setOwner(registration.getId());
fotoframz.register(registration.getDevice());
Login login = new Login();
login.setEmail(registration.getEmail());
login.setPassword(registration.getPassword());
login.setDevice(registration.getDevice());
retBean = (ServiceDataBean<AuthToken>) this.login(login);
System.out.println("form register");
} catch (Throwable e) {
retBean.setStatusCode("001");
e.printStackTrace();
}
return retBean;
}
I am getting jdbctemplate=null at int updInt = jdbcTemplate.update(newAccountSql, namedParameters, generatedKeyHolder);
my applicationContext-test.xml file is in src/test/resources folder..applicationContext-test.xml file
<context:component-scan base-package="net.mss.ff.services" />
<context:property-placeholder location="classpath:/app.properties" />
<!-- <task:annotation-driven /> -->
<context:annotation-config />
<!-- <import resource="apicontroller_v1-servlet.xml"/>
<import resource="applicationContext.xml"/> -->
<bean id="photoService" class="net.mss.ff.services.core.api.impl.PhotoServiceImpl">
<property name="rootStorageFolder" value="${storage.root}" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}" />
LoginServiceImplTest
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "classpath:/applicationContext-test.xml"})
public class LoginServiceImplTest {
LoginServiceImpl loginServiceObj = new LoginServiceImpl();
Device deviceMock;
#Autowired
private Fotoframz fotoframz;
/*private NamedParameterJdbcTemplate jdbcTemplate;
#Autowired
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}*/
#Before
public void setUp() throws Exception {
//loginServiceObj = new LoginServiceImpl();
}
#After
public void tearDown() throws Exception{
}
/**
* Test method for {#link net.mss.ff.services.core.api.impl.LoginServiceImpl#newAccount(net.mss.ff.services.core.beans.Registration)}.
*/
#Test
public void testNewAccount() {
Registration mockRegObj = new Registration();
deviceMock = new Device();
deviceMock.setActive(false);
deviceMock.setHeight(45);
//deviceMock.setId(4568);
deviceMock.setName("Android");
deviceMock.setOwner(1111);
deviceMock.setPlatform("Windows NT");
deviceMock.setUuid("522601");
deviceMock.setVersion("1.0");
deviceMock.setWidth(76);
mockRegObj.setEmail("bbb#gmail.com");
/*mockRegObj.setId(399);*/
mockRegObj.setName("bbb");
mockRegObj.setPassword("BBB");
mockRegObj.setDevice(deviceMock);
loginServiceObj.newAccount(mockRegObj);
//assertEquals("New Account Creation", "", "");
}
}
anything needs to modify in test class, please let me know..
In your test the LoginServiceImpl loginServiceObj = new LoginServiceImpl();
is not instantiated by spring, thus no annaotions will be applied. You need to autowire it, or inject it some other way. Spring 3.2 makes this kinda thing super easy and nice to use.
The rest of my answer is still good adivce :
You have not declared or instantiated the jdbctemplate in your java code. And you have not defined it in your xml file.
You need this
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dateSource"/>
</property>
</bean>
and then this as instance variable (assuming your using annoations)
#Resource(name = "jdbcTemplate")
private JdbcTemplate jdbcTemplate;
As #NimChimpsky mentioned you need to define your jdbcTemplate in your bean xml file and then in your instance variable you can also do.
#Autowired
private JdbcTemplate jdbcTemplate;

Can spring mvc trim all strings obtained from forms?

I know struts2 default config will trim all strings obtained from forms.
For example:
I type " whatever " in a form and submit, I will get "whatever" The string has been auto trimmed
Does spring mvc have this function too? THX.
Using Spring 3.2 or greater:
#ControllerAdvice
public class ControllerSetup
{
#InitBinder
public void initBinder ( WebDataBinder binder )
{
StringTrimmerEditor stringtrimmer = new StringTrimmerEditor(true);
binder.registerCustomEditor(String.class, stringtrimmer);
}
}
Testing with an MVC test context:
#RunWith(SpringJUnit4ClassRunner.class)
#WebAppConfiguration
#ContextConfiguration
public class ControllerSetupTest
{
#Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
#Before
public void setup ( )
{
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
#Test
public void stringFormatting ( ) throws Exception
{
MockHttpServletRequestBuilder post = post("/test");
// this should be trimmed, but only start and end of string
post.param("test", " Hallo Welt ");
ResultActions result = mockMvc.perform(post);
result.andExpect(view().name("Hallo Welt"));
}
#Configuration
#EnableWebMvc
static class Config
{
#Bean
TestController testController ( )
{
return new TestController();
}
#Bean
ControllerSetup controllerSetup ( )
{
return new ControllerSetup();
}
}
}
/**
* we are testing trimming of strings with it.
*
* #author janning
*
*/
#Controller
class TestController
{
#RequestMapping("/test")
public String test ( String test )
{
return test;
}
}
And - as asked by LppEdd - it works with passwords too as on the server side there is no difference between input[type=password] and input[type=text]
register this property editor:
org.springframework.beans.propertyeditors.StringTrimmerEditor
Example for AnnotionHandlerAdapter:
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
...
<property name="webBindingInitializer">
<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="propertyEditorRegistrar">
<bean class="org.springframework.beans.propertyeditors.StringTrimmerEditor" />
</property>
</bean>
</property>
...
</bean>
You can also use Spring's conversion service, which has the added benefit of working with <mvc:annotation-driven/> and with Spring Webflow. As with the other answers, the major downside is that this is a global change and can't be disabled for certain forms.
You'll need a converter to do the trimming
public class StringTrimmingConverter implements Converter<String, String> {
#Override
public String convert(String source) {
return source.trim();
}
}
Then define a conversion service that knows about your converter.
<bean id="applicationConversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="mypackage.util.StringTrimmingConverter"/>
</list>
</property>
</bean>
and tie that in to mvc.
<mvc:annotation-driven conversion-service="applicationConversionService"/>
If you use Spring Webflow then it require a wrapper
<bean id="defaultConversionService" class="org.springframework.binding.convert.service.DefaultConversionService">
<constructor-arg ref="applicationConversionService"/>
</bean>
and a setting on your flow builder
<flow:flow-builder-services id="flowBuilderServices" conversion-service="defaultConversionService" development="true" validator="validator" />
Just customized the above code in order to adjust to Spring Boot, if you want to explicit trim function for some fields in the form, you can show them as below:
#Component
#ControllerAdvice
public class ControllerSetup {
#InitBinder({"dto", "newUser"})
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
binder.registerCustomEditor(String.class, "userDto.username", new StringTrimmerEditor(false));
binder.registerCustomEditor(String.class, "userDto.password", new DefaultStringEditor(false));
binder.registerCustomEditor(String.class, "passwordConfirm", new DefaultStringEditor(false));
}
}
You can user a Spring-MVC Interceptor
public class TrimInterceptor extends HandlerInterceptorAdapter {
#Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Enumeration<String> e = request.getParameterNames();
while(e.hasMoreElements()) {
String parameterName = e.nextElement();
request.setParameter(parameterName, request.getParameter(parameterName).trim());
}
return true;
}
And set up your HandlerMapping interceptors property
<bean id="interceptorTrim" class="br.com.view.interceptor.TrimInterceptor"/>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" p:interceptors-ref="interceptorTrim"/>
}
Or use a Servlet Filter
first,trim requestparam which is String,you can create a class and implimplements WebBingdingInitializer
#ControllerAdvice
public class CustomWebBindingInitializer implements WebBindingInitializer {
#InitBinder
#Override
public void initBinder(WebDataBinder webDataBinder, WebRequest webRequest) {
webDataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}
}
please use componentScan make this Class to be a Spring Bean.
But, I don't know how to trim the String value in requestBody JSON data.

Resources