I'm trying to create a restful controller using Spring 4 MVC and testing it with Fiddler by hitting the below mentioned RestFul Post web service but getting HTTP/1.1 400 Bad Request as response, after i have added MappingJackson2HttpMessageConverter to my dispatcher-servlet.xml. The Response comes up correctly If i use '@RequestBody String resource' instead of '@RequestBody Resource resource' for ResourceController post method arguments. I've added MappingJackson2HttpMessageConverter to dispatcher-servlet.xml since spring was not detecting jackson and was throwing HTTP 415 on hitting this post service.
URL : http://localhost:8080/rma/api/resources/1
Headers :
User-Agent: Fiddler
Host: localhost:8080
Content-Length: 171
Accept: application/json
Content-Type: application/json
Request Body :
{
"firstName" : "first",
"middleName" : "middle",
"lastName" : "last",
"email": "first@last.com",
"dateOfBirth": "09-10-1988",
"titleRole" : "software engineer"
}
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU" xmlns:context="http://ift.tt/GArMu7"
xmlns:mvc="http://ift.tt/1bHqwjR"
xsi:schemaLocation="http://ift.tt/GArMu6 http://ift.tt/1jdM0fG
http://ift.tt/GArMu7 http://ift.tt/1jdLYo7
http://ift.tt/1bHqwjR
http://ift.tt/1fmimld">
<context:annotation-config />
<context:component-scan base-package="com.pkg.controller" />
<mvc:annotation-driven/>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</list>
</property>
</bean>
<bean id="resourceDao" class="com.pkg.dao.ResourceDaoImpl">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost/rma?"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
</beans>
ResourceController.class
@RestController
@RequestMapping("/resources")
public class ResourceController {
@Autowired
private final ResourceDao resourceDao;
@Autowired
public ResourceController(ResourceDao resourceDao) {
this.resourceDao = resourceDao;
}
//get resources for a resource id
@RequestMapping(value ="/{hiringManager}", method = RequestMethod.GET, produces="application/json")
public List<Resource> getResources(@PathVariable String hiringManager){
return resourceDao.getResources(hiringManager);
}
//post a new resource for a resource id
@RequestMapping(value ="/{hiringManager}",method = RequestMethod.POST, consumes="application/json",produces="application/json")
public ResponseEntity<Resource> addResources(@PathVariable String hiringManager, @RequestBody Resource resource){
System.out.println("In controller" + hiringManager + " > Adding " +resource.toString());
return new ResponseEntity<Resource>(resource,HttpStatus.CREATED);
}
}
WebApplicationInitializer.class
public class ApplicationInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext)
throws ServletException {
//Registering Dispatcher Servlet
try{
ServletRegistration.Dynamic registration = servletContext.addServlet("dispatcher", new DispatcherServlet());
registration.setLoadOnStartup(1);
registration.addMapping("/api/*"); // URL mapping
} catch(Exception e){
e.printStackTrace();
}
}
}
Aucun commentaire:
Enregistrer un commentaire