Rectangle 27 1

The converter that the error is complaining about is a JPA AttributeConverter. A GrantedAuthority is not a JPA entity, so you need a way to map it to a database column. You have 2 options:

spring - Getting error "No converter found capable of converting from ...

java spring spring-mvc spring-boot
Rectangle 27 1

No converter found for return value of type: class java.lang.String

It says that the MVC doesn't know which converter use to return the REST response from this service. to use the default converter which are available in spring.

Please add an annotation @ResposeBody in the method signature.

string - MessageTemplate cause no converter found exception in MVC con...

string spring spring-mvc converter
Rectangle 27 0

Im glad you got it sorted anyway. I pulled the project and ran it myself anyway just to take a look and your explanation is correct. You could add this to your log4j.xml if you wanted to get rid of that message from polluting your logs in the future :-)

<logger name="org.springframework.beans.TypeConverterDelegate">
    <level value="error" />
</logger>

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

Spring MVC "No converter found capable of converting from type java.la...

java spring spring-mvc type-conversion
Rectangle 27 0

Turns out this was just me not reading the line above the stack trace properly... missed the fact that the line above the stack was "DEBUG".

In short, the conversion service is unable to convert from a String to a Resource; however, spring falls back on it's PropertyEditor-based conversion which succeeds (I believe this is how String-Resource conversions are done normally).

Example of the full stack was as follows:

DEBUG: org.springframework.beans.TypeConverterDelegate - Original ConversionService attempt failed - ignored since PropertyEditor based conversion eventually succeeded
org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.util.ArrayList<?> to type java.util.List<org.springframework.core.io.Resource> for value '[/WEB-INF/css/]'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type java.lang.String to type org.springframework.core.io.Resource
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41)
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:168)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:161)
    at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:448)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:494)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:488)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1463)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1422)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1158)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:633)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:602)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:521)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:462)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5198)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5481)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:634)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:671)
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1840)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type java.lang.String to type org.springframework.core.io.Resource
    at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:276)
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:171)
    at org.springframework.core.convert.support.CollectionToCollectionConverter.convert(CollectionToCollectionConverter.java:83)
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:35)
    ... 40 more

Spring MVC "No converter found capable of converting from type java.la...

java spring spring-mvc type-conversion
Rectangle 27 0

For my opinion you've encounterd next situation here. When Spring trying to deserialize Employee entity which was received in addNewHandle it found property department of type of String but in target entity it has Department type, then, because you have no conversion regsieterd for such kind of conversion, it fails. So, to solve this issues you can implement own converter (Converter) that gets String and returns Department and regsiter it in conversionService or you can implement own JSON deserializing strategy by overriding Jackson JsonDeserializer and annotating department property with @JsonDeserialize(using=<your Jacson custom deserializer>.class). Hope this helps.

JSON has no place here. This is a form and the <input> elements will be url-encoded form parameters.

I don't recommend doing this in a Converter. You would probably want to retrieve the Department and create the Employee in the same transaction. It would be hard to do this if you retrieved the Department in a Converter.

While Spring deserialize form into target entity, it creates target entity object and then iterates overtform properties and trying set appropriate fields in target entity object. While setting target entity object field Spring try to convert from source form property value to target field value by means of type conversion and can use appropriate Converter. Thus, we don't need Converter for whole Employee entity, but we need Converter for department property. Correct me if i'm wrong. Thanks in advance.

That is correct. In this case, the Employee is a new object, not yet persisted. But we can assume that the Department exists in persistent storage and that's the one OP is referring to.

java - How to receive complex objects in spring 3.2 mvc? - Stack Overf...

java spring-mvc data-binding
Rectangle 27 0

You have to let Spring know how to convert the return Object by you exception handler so it can write to HTTP response. Lets say "ErrorTO" is a JAXB object and then returned content type is application/xml you should create a HandlerExceptionResolver in your application context and configure a message converter that support application/xml content type (e.g. org.springframework.http.converter.xml.MarshallingHttpMessageConverter). here is an example:

<bean id="outboundExceptionAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver">
    <property name="messageConverters">
      <util:list>
        <ref bean="marshallingHttpMessageConverter"/>
      </util:list>
    </property>
  </bean>

  <bean id="marshallingHttpMessageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
    <property name="marshaller" ref="jaxb2Marshaller" />
    <property name="unmarshaller" ref="jaxb2Marshaller" />
  </bean>

  <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="contextPath" value="com.acme" />
  </bean>

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

java - Can a Spring MVC exception handler ONLY support return type of ...

java spring spring-mvc
Rectangle 27 0

For my opinion you've encounterd next situation here. When Spring trying to deserialize Employee entity which was received in addNewHandle it found property department of type of String but in target entity it has Department type, then, because you have no conversion regsieterd for such kind of conversion, it fails. So, to solve this issues you can implement own converter (Converter) that gets String and returns Department and regsiter it in conversionService or you can implement own JSON deserializing strategy by overriding Jackson JsonDeserializer and annotating department property with @JsonDeserialize(using=<your Jacson custom deserializer>.class). Hope this helps.

JSON has no place here. This is a form and the <input> elements will be url-encoded form parameters.

I don't recommend doing this in a Converter. You would probably want to retrieve the Department and create the Employee in the same transaction. It would be hard to do this if you retrieved the Department in a Converter.

While Spring deserialize form into target entity, it creates target entity object and then iterates overtform properties and trying set appropriate fields in target entity object. While setting target entity object field Spring try to convert from source form property value to target field value by means of type conversion and can use appropriate Converter. Thus, we don't need Converter for whole Employee entity, but we need Converter for department property. Correct me if i'm wrong. Thanks in advance.

That is correct. In this case, the Employee is a new object, not yet persisted. But we can assume that the Department exists in persistent storage and that's the one OP is referring to.

java - How to receive complex objects in spring 3.2 mvc? - Stack Overf...

java spring-mvc data-binding
Rectangle 27 0

Spring 3 introduced the Converter SPI which makes this quite easy. Have a look at 6.5 in the documentation

Taking source from the docs and putting in your country, you would do something like

package my.converter;

final class StringToCountry implements Converter<String, Country> {
    public Country convert(String source) {
        return // whatever you do to get a country from your string
    }
}

Then in the xml config you would configure the converter

<bean id="conversionService"
      class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
        <list>
            <bean class="my.converter.StringToCountry"/>
        </list>
    </property>
</bean>

As GriffeyDog pointed out, you may want to put the country id in for the select path so you can get the Country by ID or something instead of whatever is returned by toString() of your Country object.

Spring form binding how to do it ? Cannot convert value of type [java....

java spring spring-mvc
Rectangle 27 0

I just had the error with Spring 4.1.1 and the same definition of FormattingConversionServiceFactoryBean.

Hiding logs won't resolve the issue... and in my opinion, it's not a viable solution, especially for production.

<set></set>
<list></list>

Spring MVC "No converter found capable of converting from type java.la...

java spring spring-mvc type-conversion
Rectangle 27 0

The issue is that you need to register a property editor or converter which is capable of converting your account type names to the AccountType object. Your view send a string value for the accountTypes element. Spring needs to know how to bind the String value to the Object.

public class AccountTypeEditor extends PropertyEditorSupport{

   public void setAsText(String text){
        AccountType accountType = // some logic to find account type by name
        setValue(accountType);
   }
}
@InitBinder
public void initBinder(WebDataBinder binder){
   binder.registerCustomEditor(AccountType.class , new AccountTypeEditor());
}

thanks, I'll try to do as you said

I was able to solve the problem, if interested can look at. I made a reply

java - MVC and Object with Set<> - Stack Overflow

java spring hibernate jsp spring-mvc
Rectangle 27 0

The problem is that you are assuming that Spring MVC is going to be able to fill your Apartment object from the data passed. From the form it looks like the Building number is 12, which probably is a unique identifier for the Building in the database, but how is Spring MVC going to know how to go to the database, retrieve the proper building object and put it into the Apartment object?

Remember that objects mapped through SpringMVC parameters are regular Java POJOs, not Hibernate attached entities. So, when the mapping occurs SpringMVC is trying to put "12" into the building attribute of type Building into your POJO (which explains the error you are getting).

You have two options: First, you can register a custom formatter, that will use the passed id to retrieve a Building from the database:

import org.springframework.core.convert.converter.Converter;

public class BuildingIdToBuildingConverter implements Converter<String, Building> {

    private BuildingService buildingService;

    public BuildingIdToBuildingConverter(BuildingService buildingService) {
        this.buildingService = buildingService;
    }

    @Override
    public Building convert (String id) {
            return buildingService.getBuilding(id);
    }
}
public class AppConfig extends WebMvcConfigurerAdapter {

    ...

    @Bean
    public BuildingService buildingService(){
        return new BuildingService();
    }

    @Override
    public void addFormatters (FormatterRegistry registry) {
        registry.addConverter(new  BuildingIdToBuildingConverter(buildingService()));
    }
}

Second, do this work manually by sending the building id in a separate parameter:

@RequestMapping(value = "admin/addApartment", method = RequestMethod.POST)
   public String addContact(@ModelAttribute("apartment") Apartment apartment, @RequestParam("buildingId") String buildingId, BindingResult result, HttpServletRequest request, Map<String, Object> map) {
       if (result.getErrorCount()==0){
           if (apartment.getId()==0){
               apartment.setBuilding(buildingService.getBuilding(buildingId));
               apartmentService.addApartment(apartment);
            }
        }
       else{
           apartmentService.editApartment(apartment);
       }

       return "redirect:/admin/apartment.html";
   }

   map.put("buildingList", buildingService.listBuilding());
   map.put("apartmentList", apartmentService.listApartment());
   return "apartment";
}

Spring - Failed to convert property value of type java.lang.String to ...

spring spring-mvc