Reputation: 525
I can't fiure out what I am doing wrong here. I am using the app "Postman" to send a request to a service. The parameter is a very simple POJO shown below. When I attempt to send the request I get a response: "The server refused this request because the request entity is in a format not supported by the requested resource for the requested method"
Class used for request:
public class LoginAttempt {
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
API in controller:
@RequestMapping(value="/validate", method=RequestMethod.POST, produces="application/json")
public boolean validateUser(@RequestBody LoginAttempt login) {
System.out.println("Login attempt for user " + login.getUserName() + login.getPassword());
return true;
}
Upvotes: 0
Views: 1164
Reputation: 2626
FormHttpMessageConverter which is used for @RequestBody-annotated parameters when content type is application/x-www-form-urlencoded cannot bind target classes as @ModelAttribute can). Therefore you need @ModelAttribute instead of @RequestBody
Either Use @ModelAttribute annotation instead of @RequestBody like this
public boolean validateUser(@ModelAttribute LoginAttempt login)
or you can create a separate method form processing form data with the appropriate headers attribute like this:
@RequestMapping(value="/validate", method=RequestMethod.POST, headers = "content-type=application/x-www-form-urlencoded" produces="application/json")
Upvotes: 2
Reputation: 1593
Just try adding this bean:
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="prefixJson" value="false"/>
<property name="supportedMediaTypes" value="application/json"/>
</bean>
Upvotes: 1