2

I am making SPA using AngularJS in Spring 4 with Hibernate 5. I can't send a request from the AngularJS controller to the Spring Controller.

execution coming inside submit() function successfully, but it's going fail.

Error shows in browser console:

POST http://localhost:8050/Spring_Hibernate_MVC/Views/registerStudent 404 (Not Found)

My project structure is like below.

Spring_Hibernate_MVC
=src
-com->karmesh->mvcApp->controller->register->RegisterController.java
=WebContent
-js->app->RegisterController.js
-Views->Register.html

Register.html

<div id="DivRegisterMain" ng-controller="RegisterController as ctrl">

    <form name="myForm" novalidate>
    :
    :
    :
    <input type="submit" value="Submit" ng-click="submit()" ><br>
</form>
</div>

app.js

var routeApp=angular.module("RouteApp",['ngRoute']);

RegisterController.js

routeApp.controller("RegisterController", function($scope, $http) {
:::://some code is here
$scope.submit = function() {

        var req = {
                 method: 'POST',
                 url: '/registerStudent',               
                 data: $scope.studentList
                };

        $http(req).
        then(function(response){
            console.log(response.status);
            console.log("in success");
            $scope.studentList=[];
        }, function(response){
            console.log(response.status);
            console.log("in fail");     
        });
    };
});

web.xml

<servlet>
        <servlet-name>springServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

RegisterController.java

@RestController
@RequestMapping("/")
public class RegisterController {

    @Autowired
    private RegisterService registerService;


    @RequestMapping(value="/registerStudent", method = RequestMethod.POST)  
    public ResponseEntity<RegisterDTO> registerStudent(@RequestBody List<RegisterDTO> stdList) {    


        if (registerService.isStudentExist(stdList)) {
            return new ResponseEntity<RegisterDTO>(HttpStatus.CREATED);
        }

        return new ResponseEntity<RegisterDTO>(HttpStatus.CONFLICT);

    }
}
Elydasian
  • 2,016
  • 5
  • 23
  • 41

1 Answers1

0

[EDIT adding ".do", changing method return, adding parameter check]

You are requesting a wrong URL.

Replace

http://localhost:8050/Spring_Hibernate_MVC/Views/registerStudent 

With

http://localhost:8050/Spring_Hibernate_MVC/registerStudent.do //notice I removed /views/ and added .do

If even the second one doesn't work try

http://localhost:8050/springServlet/registerStudent.do 

So in your angular controller change the url to be like below

var req = {
        method: 'POST',
        url: 'http://localhost:8050/Spring_Hibernate_MVC/registerStudent.do',//notice the .do at the end ro try http://localhost:8050/springServlet/registerStudent.do             
        data: $scope.studentList
};

RegisterController

@Controller  //LOOK here
@RequestMapping("/")
public class RegisterController {

    @Autowired
    private RegisterService registerService;

    @ResponseBody  //LOOK here
    @RequestMapping(value="/registerStudent", method = RequestMethod.POST)  
    public boolean registerStudent(@RequestBody List<RegisterDTO> stdList) {    
       if (stdList != null) {   //check if the object received is not null
         //store your stdList somehow
      }
      return registerService.isStudentExist(stdList);
    }
}

Your AngularController

$scope.submit = function() {
var req = {
        method: 'POST',
        url: 'http://localhost:8050/Spring_Hibernate_MVC/registerStudent.do',               
        data: $scope.studentList
};

$http(req).then(function(response){
    console.log(response); // prints true or false
    if (response)
      console.log("in success");
    else 
       console.log("in fail");
    $scope.studentList=[];
    }, function(error){
     console.log("in fail");     
    });
};

});

hzitoun
  • 5,492
  • 1
  • 36
  • 43
  • it's not working, Error: angular.js:12011 POST http://localhost:8050/Spring_Hibernate_MVC/registerStudent 404 (Not Found) –  Oct 28 '16 at 05:21
  • @KarmeshMadhavi I've updated my solution, I've added .do at the end of your URL – hzitoun Oct 28 '16 at 07:21
  • now i'm getting another error, Unsupported Media Type 415 ......Content type 'application/json;charset=UTF-8' not supported ......what is this –  Oct 28 '16 at 11:43
  • @KarmeshMadhavi you are missing the following dependencies org.codehaus.jackson jackson-mapper-asl 1.9.13 com.fasterxml.jackson.core jackson-databind 2.5.3 Kindly add them and let me know if it works – hzitoun Oct 28 '16 at 11:50
  • No, it's not working, still same error, i update project, and try also in new workspace... –  Oct 28 '16 at 12:30
  • @KarmeshMadhavi please add `@EnableWebMvc` to your spring **config** java class – hzitoun Oct 28 '16 at 12:36
  • here i'm getting new error....Could not read JSON: ; nested exception is com.google.gson.JsonSyntaxException: ........i'm passing JSON array to the POJO type List, please check the above **RegisterController**, it is the way to store array..? can you suggest me? –  Oct 28 '16 at 14:48
  • @KarmeshMadhavi I've updated my answer, kindly take a look and let me know if it worked – hzitoun Oct 28 '16 at 15:27
  • yes, previous error is gone, thank you i appreciate ,but how i confirm that, the array object is coming to the controller.?.....so if i will store that array in LIST, then it will be sure that the data is passed successfully. –  Oct 28 '16 at 17:02
  • @KarmeshMadhavi Please consider accepting and upvoting my solution since it fixes your base problem (404 not found). Now about your last question, I'll make another edit to my answer. – hzitoun Oct 28 '16 at 19:40
  • i upvote your solution but, still i don't have much reputation i can't see....... @EnableMvc is required right,......and still i'm getting errorHandler execution resulted in exception: Could not read JSON: ; nested exception is com.google.gson.JsonSyntaxException: ............ can we chat some where instead of post comment here...? –  Oct 30 '16 at 15:02