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

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.

Related

BeanNotOfRequiredTypeException with PlatformTransactionManager

I'm new to Spring and I did a login/register applicaton following a youtube tutorial but I want to add a new functionality that allows to delete a student. I used #Transactional on my delete method and modified accordingly the xml file but I get this error:
Message Request processing failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'platformTransactionManager' is expected to be of type 'org.springframework.transaction.PlatformTransactionManager' but was actually of type 'com.infotech.service.impl.StudentServiceImpl'
my Service class
#Service("studentService")
public class StudentServiceImpl implements StudentService {
#Autowired
private StudentDAO studentDAO;
public void setStudentDAO(StudentDAO studentDAO) {
this.studentDAO = studentDAO;
}
public StudentDAO getStudentDAO() {
return studentDAO;
}
//other methods
#Override
public void delete(String email) {
getStudentDAO().delete(email);
}
}
my DAO class
#EnableTransactionManagement
#Repository("studentDAO")
public class StudentDAOImpl implements StudentDAO {
#Autowired
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
#Autowired
private SessionFactory sessionFactory;
protected Session getSession() {
return (Session) sessionFactory.getCurrentSession();
}
//other methods
#Transactional("platformTransactionManager")
public void delete(String email) {
Student student = (Student) ((HibernateTemplate) getSession()).get(Student.class, email);
((HibernateTemplate) getSession()).delete(student);
}
}
In the dispatcher servlet I have defined InternalResourceViewResolver, dataSource, hibernateTemplate, sessionFactory beans and then I added another bean
<tx:annotation-driven transaction-manager="platformTransactionManager"/>
<bean id= "platformTransactionManager"class="com.infotech.service.impl.StudentServiceImpl">
</bean>
Finally, this is the controller
#Controller
public class MyController {
#Autowired
private StudentService studentService;
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
public StudentService getStudentService() {
return studentService;
}
//...RequestMappings...
#RequestMapping(value = "/delete/{email}", method = RequestMethod.GET)
public ModelAndView delete(#PathVariable("email") String email) {
studentService.delete(email);
return new ModelAndView("redirect:/view/home");
}
...
}
Now, how can I make my bean of PlatformTransactionManager type?
But most of all I think there's a simpler way to delete a field from my table, maybe without using #Transaction at all so can anyone help me understand why I get the error and explain me what is #Transactional and if I really should use it in this case?
Remember that I'm NEW to Spring, I still don't have many notions so sorry if I wrote something totally stupid :-)
Spring is looking for transaction manager - it requires a concrete implementation of the PlatformTransactionManager interface. It's being given your service implementation, which isn't a PlatformTransactionManager and not what it needs. If you're using JDBC, org.springframework.jdbc.datasource.DataSourceTransactionManager should work.
Try changing:
<bean id= "platformTransactionManager" class="com.infotech.service.impl.StudentServiceImpl">
To:
<bean id= "platformTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

Spring data MongoDb cannot convert proxy bean

I'm using Spring AOP with AspectJ and Spring Data MongoDb and am having a world of trouble persisting objects.
In this case, I have an AclEntryDaoImpl that exposes AclEntryImpl. When AclEntryImpl is provided a Principal that is a standard Java object (a "non-Spring" bean), mongoTemplate.save() works as expected. However when Principal is a Spring bean, Mongo is unable to convert the object and results in a MappingException org.springframework.data.mapping.model.MappingException: No id property found on class class com.sun.proxy.$Proxy33. All my objects need to be Spring beans so that (a) I keep my objects decoupled and (b) my AOP (LoggingAspect) is invoked.
Lastly, I cannot take advantage of Spring converters because Mongo sees the target object AclEntryImpl as a proxy com.sun.proxy.$Proxy33 and so Converter<Principal, DBObject> is never invoked.
Any and all help would be greatly appreciated!
Snippets:
Here's my Spring XML configuration:
<beans>
<context:component-scan base-package="a.b" />
<context:property-placeholder location="config.properties" />
<aop:aspectj-autoproxy />
<bean id="loggingAspect" class="a.b.LoggingAspect" />
<mongo:db-factory host="${database.host}" port="${database.port}" dbname="${database.dbname}" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
<bean id="aclEntryDao" class="a.b.AclEntryDaoImpl">
<lookup-method name="createAclEntry" bean="aclEntry" />
</bean>
</beans>
AclEntryImpl:
#Document
#Component
#Scope("prototype")
public class AclEntryImpl implements AclEntry {
#Id
private String id;
private String service;
#DBRef #Expose
private Principal principal;
#Expose
private boolean accessGranted;
#Expose
private List<Permission> permissions;
#Override #Loggable #MongoSaveReturned
public AclEntry save() {
return this;
}
...getters and setters...
}
AclEntryDaoImpl:
#Repository
public abstract class AclEntryDaoImpl implements AclEntryDao {
#Override #Loggable
public AclEntry addEntry(String serviceName, Principal principal, Permission[] permissions, boolean accessGranted) throws Exception {
AclEntry entry = createAclEntry(); //<-- Spring lookup-method
entry.setService(serviceName);
entry.setPrincipal(principal); //<-- com.sun.proxy.$Proxy33
entry.setAccessGranted(accessGranted);
for (Permission permission : permissions) {
if (!entry.addPermission(permission)) {
return null;
}
}
return entry.save();
}
... other DAO methods ...
}
LoggingAspect:
#Aspect
public class LoggingAspect {
#Autowired
private MongoTemplate mongoTemplate;
#Pointcut("execution(!void a.b..*.*(..))")
public void returningMethods() {}
#AfterReturning(pointcut="returningMethods() && #annotation(MongoSaveReturned)", returning="retVal")
public Object mongoSaveReturnedAdvice(Object retVal) {
Logger logger = null;
try {
logger = getLogger(retVal);
mongoTemplate.save(retVal); //<-- throws MappingException
log(logger, "save: " + retVal.toString());
} catch (Exception e) {
log(logger, "throw: " + e.toString());
}
return retVal;
}
... other logging methods ...
}

how to call method using spring?

I want to call a method after creating the bean using spring, for example I create Factory and schema but I want to call the same method of Factory before creating a schema bean
<!-- schemaFactory-->
<bean id="schemaFact" class="javax.xml.validation.SchemaFactory"
factory-method="newInstance">
<constructor-arg value="http://www.w3.org/2001/XMLSchema" />
</bean>
<!-- schema -->
<bean id="schema" class="javax.xml.validation.Schema"
factory-bean="schemaFact" factory-method="newSchema">
<constructor-arg value="3DSecure.xsd" />
</bean>
You could use your own factory bean instead of javax.xml.validation.SchemaFactory, which will delegate to javax.xml.validation.SchemaFactory and the call the method:
public class MySchemaFactory {
public SchemaFactory newInstance() {
SchemaFactory factory = SchemaFactory.newInstance();
factory.callSomeMethod();
return factory;
}
}
<bean id="mySchemaFactory" class="com.foo.bar.MySchemaFactory"/>
<bean id="schemaFact"
factory-bean="mySchemaFactory"
factory-method="newInstance"/>
If you are using Spring 3.0/3.1 you can take advantage of Java configuration:
#Configuration
class Config {
#Bean
public SchemaFactory schemaFact() throws SAXNotSupportedException, SAXNotRecognizedException {
final SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
schemaFactory.setFeature("apache.org/xml/features/validation/schema-full-checking", false);
return schemaFactory;
}
#Bean
public Schema schema() throws SAXException {
return schemaFact().newSchema(new File("3DSecure.xsd"));
}
}
But which method on SchemaFactory do you want to call? Seems like all of them are either getters or setters, so you can use normal XML injection... Alternatively create your own FactoryBean:
class SchemaFactoryFactoryBean implements FactoryBean<SchemaFactory> {
#Override
public SchemaFactory getObject() throws Exception
{
final SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
schemaFactory.setFeature("apache.org/xml/features/validation/schema-full-checking", false);
return schemaFactory;
}
#Override
public Class<?> getObjectType()
{
return SchemaFactory.class;
}
#Override
public boolean isSingleton()
{
return true;
}
}
And use it like this (no factory-method needed):
<bean id="schemaFact" class="SchemaFactoryFactoryBean"/>

How Spring instantiate this class and its dependancy

Not sure this is a spring related or not:
I have a bean class like this
class BeanClass {
private ServiceA serviceA;
private ServiceB serviceB;
public BeanClass() {}
public void setServiceA(ServiceA serviceA) {
this.serviceA = serviceA;
}
public void setServiceB(ServiceB serviceB) {
this.serviceB = serviceB;
}
public void callService() {
serviceA.a();
serviceB.b();
}
}
and its configuration:
<bean id="beanClass" class="BeanClass">
<property name="serviceA" ref="serviceA"/>
<property name="serviceB" ref="serviceB"/>
</bean>
but I instantiate the bean with new keyword in a controller class:
class ControllerClass {
public void someMethod() {
BeanClass beanClass = new BeanClass();
beanClass.callService();
}
}
my problem is how ServiceA and ServiceB got instantiated properly? since I use new to got a class object, and never set its field member.
You don't want to do:
BeanClass beanClass = new BeanClass();
This in a nutshell is the whole point of Spring's IOC container. You should let the IOC container give you a reference to your BeanClass
#Autowired
BeanClass myBeanClass;
*This isn't entirely complete as you'll need a bit of extra wiring for your controller.

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