Q:

Struggling with CORS error using Spring,Angularjs and Ionic frame work

Controller.java
In Controller class,I have defined url and while accessing from browser its working fine.
But when accessing the same url request from Ionic program, getting CORS error.Due to this error i have added necessary content in doFilter method too.But still left helpless.
@RestController
@RequestMapping("/service")
public class Controller implements Filter {

@RequestMapping(value = "/name", method = RequestMethod.POST,
        headers = "content-type=application/x-www-form-urlencoded",produces="application/json")
public String reg(@ModelAttribute Farmer farmer) {

    System.out.println("Getting request from Mobile client...!!" + farmer);//farmer.firstName is null
    String result = "Hello..!";
    System.out.println("Request processed");
    return result;
}
@Override
public void destroy() {
    // TODO Auto-generated method stub
}

@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
        throws IOException, ServletException {
    HttpServletResponse response = (HttpServletResponse) resp;
    System.out.println("External request start..!!");
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "POST, PUT,GET,DELETE");
    response.setHeader("Access-Control-Max-Age", "3600");
    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");

    chain.doFilter(req, resp);
    System.out.println("External request end..!!");
}

@Override
public void init(FilterConfig arg0) throws ServletException {
    // TODO Auto-generated method stub

}

Farmer.java
public class Farmer {
private String firstName;
private String lastName;
//getters and setters
}

I have edited my question.Please do check it.
Thank you.

A:

After going with lot of trials and referring examples.Finally fixed this issue.So just thought of adding here.If in case anyone might need it.

This fixes just by adding
  @CrossOrigin,@RestController,consumes="application/json" in your
  controller method.

@CrossOrigin
@RestController
@RequestMapping("/service")
public class Controller implements Filter {
@RequestMapping(value = "/name", method = RequestMethod.POST,
    headers = "content-type=application/x-www-form-urlencoded",consumes="application/json")

public String reg(@ModelAttribute Farmer farmer) {
System.out.println("Getting request from Mobile client...!!" + farmer);//farmer.firstName is null
String result = "Hello..!";
System.out.println("Request processed");
return result;
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}

@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
    throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) resp;
System.out.println("External request start..!!");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, PUT,GET,DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");

chain.doFilter(req, resp);
System.out.println("External request end..!!");
}

@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}

Thank you

