Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeMismatchException instead of IllegalArgumentException: argument type mismatch for wrong RequestBody [SPR-7263] #11922

Closed
spring-projects-issues opened this issue Jun 6, 2010 · 5 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Gerrit Brehmer opened SPR-7263 and commented

If a controller method is called with a wrong (but for message converter known type) @RequestBody message, an java internal reflection exception will be raised: java.lang.IllegalArgumentException: argument type mismatch.

It would be nice, if a Spring-TypeMismatchException exception could be raised instead (so a type check is needed). This exception is already mapped to a 400 Bad Request. With IllegalArgumentException the cause of the exception is not exactly enough.


Affects: 3.0.2

Attachments:

Referenced from: commits 65885d1, 723f94f

@spring-projects-issues
Copy link
Collaborator Author

Arjen Poutsma commented

I'm having difficulties reproducing this issue. Can you illustrate when this issue occurs? Specifically, I'm interested in the @Controller method signature, and the payload (content-type and contents) that triggers this behavior. It would be great if you could attach a unit test that reproduces this, but it's not strictly necessary.

@spring-projects-issues
Copy link
Collaborator Author

Gerrit Brehmer commented

Controller:

@RequestMapping(value="/customer/{id}/address", method = RequestMethod.PUT)
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void changeAddress(@PathVariable("id") Long id, @RequestBody Address address, HttpServletRequest request) {
}

@RequestMapping(value="/customer/{id}/bankaccount", method = RequestMethod.PUT)
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void changeBankAccount(@PathVariable("id") Long id, @RequestBody BankAccount bankAccount, HttpServletRequest request) {
}

If I call "customer/123/bankaccount" with an address-instances as xml payload, the Payload will be converted by the MessageConverter but is the wrong type for function call. The MarshallingHttpMessageConverter is not using provided requestbody class type instead of other HttpMessageConverter.

Http-Request:

PUT /app/customer/38961805/address HTTP/1.1
Content-Length: 27
Content-Type: application/xml 

<bankAccount></bankAccount>

Spring-config:

<bean
     class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
     <property name="messageConverters">
          <list>
               <ref bean="marshallingHttpMessageConverter" />
          </list>
     </property>
</bean>
<bean id="marshallingHttpMessageConverter"
     class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
     <property name="marshaller" ref="marshaller"></property>
     <property name="unmarshaller" ref="marshaller"></property>
</bean>
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
     <property name="classesToBeBound" ref="jaxbxmlrootlist" />
</bean>

I have more time tonight and can then provide a unittest.

@spring-projects-issues
Copy link
Collaborator Author

Gerrit Brehmer commented

Attached a unittest. During the configuration of the test i saw, that Jaxb2RootElementHttpMessageConverter unlike MarshallingHttpMessageConverter throws the right exception (MessageNotReadable), because it creates a new JAXBContext/Marshaller for each request, which only supports the type of the @RequestBody method parameter.

@spring-projects-issues
Copy link
Collaborator Author

Arjen Poutsma commented

Thanks for the unit test! That certainly helps.

@spring-projects-issues
Copy link
Collaborator Author

Arjen Poutsma commented

Fixed now. The unit test really helped, thanks for that!

@spring-projects-issues spring-projects-issues added type: enhancement A general enhancement in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 3.0.3 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants