404 not found error in jQuery AJAX call for Spring controller

I am new to Spring. I am generating JSON as shown below:

[
    {
        "customer" : "16", "project" : "19",
        "phase" : "47", "approver" : "5",
        "date1" : "", "date2" : "",
        "date3" : "", "date4" : "",
        "date5" : "", "date6" : "",
        "date7" : "", "activity" : "1"
    },

    {
        "customer" : "17", "project" : "20",
        "phase" : "48", "approver" : "3",
        "date1" : "", "date2" : "",
        "date3" : "", "date4" : "",
        "date5" : "", "date6" : "",
        "date7" : "", "activity" : "1"
    }
]

I passed this JSON to my Spring controller:

$.ajax({
    type: 'post',
    url: 'NewTimesheet',
    dataType : 'json',
    data: JSON.stringify(jsonObj),

    success: function(data) {
        console.log(data);
    }
});

I matched the request to the controller as shown below:

@RequestMapping(value="NewTimesheet", headers = { "Content-type=application/json" })
@ResponseBody
public String addNewTimesheet(@RequestBody List<Timesheet> timesheet,
    HttpSession session) {

    logger.info("timesheet list size is" + timesheet.size());
    return "success";
}

Timesheet class:

public class Timesheet {
    private String project;
    private String phase;
    private String approver;
    private String customer;
    private String date1;
    private String date2;
    private String date3;
    private String date4;
    private String date5;
    private String date6;
    private String date7;
    private String activity;

    //Getters and setters
}

But my request does not map to the controller. My console is displayed as below:

WARN org.springframework.web.servlet.PageNotFound.handleNoSuchRequestHandlingMethod: 142 - : path '/NewTimesheet', POST, [ '[{ "": "16", "": "19", "": "47", "": "5", "date1": "," date2": "," date3": "," date4": "," date5": "," date6": "," date7": "," ":" 1"}, { "": "17", "": "20", "": "48", "": "3", "date1": "," 2": "," date3 ":" "," date4 ":"," date5": "," date6": "," date7": "," ":" 1"}] → array ['']]

JSON ? .

+4
5

, RequestMapping HTTP .

@Controller
@RequestMapping("/NewTimesheet")
public class MyClassName {

        @RequestMapping(value={ "", "/" }, method = RequestMethod.POST, headers = { "Content-type=application/json" })
        @ResponseBody
        public String addNewTimesheet(@RequestBody List<Timesheet> timesheet,HttpSession session){
                 logger.info("timesheet list size is"+timesheet.size());
                 return "success";
        }
}
+3

@RequestBody @ModelAttribute . json, "". , . , ..... . .

+2

, :

  • , . Maven :

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.2.2</version>
    </dependency>
    
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.2.2</version>
    </dependency>    
    
  • JavaScript-, . , :

    $.ajax({
        method : 'post',
        url : 'NewTimesheet',
        dataType : 'json',
        data:{ 'timesheet': jsonObj },
        success : function(data) {
            console.log(data);
    
        }
    });
    
  • , POST. . , , , :

    @RequestMapping(value="NewTimesheet", method = RequestMethod.POST)
    public @ResponseBody String addNewTimesheet(@RequestParam("timesheet") Timesheet[] timesheet,HttpSession session){
             logger.info("timesheet list size is"+timesheet.length);
             return "success";
    }
    

Spring MVC, JSON . AJAX JSON, Spring Jackson .

+2

method=post. produces = "application/json"

@RequestMapping(value="NewTimesheet", method = RequestMethod.POST, produces = "application/json")
        @ResponseBody
        public String addNewTimesheet(@RequestBody List<Timesheet> timesheet,HttpSession session){
                 logger.info("timesheet list size is"+timesheet.size());
                 return "success";
        }
+2

In my ajax request, I added contentType: application / json. After adding this controller, I matched my ajax request. Thanks for all.

+1
source

All Articles