HI,
I have Spring applications built where in I am able to retrieve the response when using simple request parameters in the request coming from UI.However when trying to replace it with @RequestBody with passing a json in the request.It gives me 405 "Method not allowed error".
Here is the code of web.xml file
<
servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
spring_servlet.xml
<context:component-scan base-package="com.dataguise.dgcontrol.servlets" />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="contentType" value="application/json"/>
</bean>
<bean id="jsonConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
My Spring controller class has two methods
@Controller
public class DgCentralControllerServlet {
DgControlRepository repo = new DgControlRepository();
///Method 1
@RequestMapping(value ="/LoadDiscoverExpression")
public @ResponseBody List<DgPolicyRegexPatternsUI> getMovie(@RequestParam("id") Integer id ,@RequestParam("source")
String source, HttpServletResponse response,
HttpServletRequest request) throws DgException{
List<DgPolicyRegexPatternsUI> dgRegexPatternsList = repo.loadPolicyRegexPatternsTest(id,source, "All");
return dgRegexPatternsList;
}
/// Method 2
@RequestMapping(value ="/LoadDiscover", method=RequestMethod.POST, headers={"Accept=*/*", "Content-Type=application/json"})
public @ResponseBody DgDiscoverExpressionPol getexp(@RequestBody DgDiscoverExpressionPol pol,Model model) throws Exception{
DgDiscoverExpressionPol returnpol = new DgDiscoverExpressionPol();
returnpol.setSource(pol.getSource());
return returnpol;
}
Method 1 works and gives me response when running the URL on the browser using RESTCLIENT addon on mozilla
http://localhost:8080/dgControl/LoadDiscoverExpression?id=0&source=structured
However when using the Method 2 on the URL
http://localhost:8080/dgControl/LoadDiscover?json={"source":"structured"}
It gives me 405 Method not allowed error