0

I am a beginner with spring and angular. I made a controller class in com.test.alex named Search. In my Search class i have this java code:

package com.siemens.cvc.isrules.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class Search {

    @RequestMapping(value = "/getHelloWord", method = RequestMethod.GET)
    public @ResponseBody String getHelloWord(ModelMap model){
        String jsonData ="[{\"salut\":\"Hello Word\"}]";
        return jsonData;
    }

}

Then I have a UI with the following button:

<div class="input-group-addon" ng-click="doSearch();"><span class="glyphicon glyphicon-search"></span></div>

When i press that button, i want to make a request to "/getHelloWord" (Search class) and alert the response.

This is the angularjs controller:

app.controller("search",function($scope,$http){
                    $scope.doSearch = function()
                    {
                        $http({
                            method : 'GET',
                            url : '/ISRulesTool/getHelloWord'
                        }).success(function(data,status,headers,config){
                            alert(data);
                        }).error(function(data,status,headers,config){
                            alert("Request Error");
                        });
                    };
});

And the problem i get is 404. "GET http://localhost:8080/ISRulesTool/getHelloWord 404 (Not Found)"

Where I need to make the request to get the response?

Thank you.

5
  • Sorry the package is com.siemens.cvc.isrules.controller Commented Jan 7, 2016 at 13:05
  • Where did you declare the path /ISRulesTool ? Commented Jan 7, 2016 at 13:07
  • 2
    You could use @RestController instead of @Controller, and lose the @ResponseBody on all your methods. But that's not the problem here. And note that, with AngularJS >= 1.4.4, the use of .success() and .error() is deprecated (see : docs.angularjs.org/api/ng/service/$http). Commented Jan 7, 2016 at 13:09
  • 2
    did you check if your GET request is working with some REST client like RESTeasy or Postman ? Issue can be in your Spring layer. Put a debug point or a sysout in your getHelloWorld() and see if it's coming when you hit your GET request Commented Jan 7, 2016 at 13:16
  • 1
    Is this URL http://localhost:8080/ISRulesTool/getHelloWord working from any REST client in browser ? Commented Jan 7, 2016 at 14:03

2 Answers 2

1

I think "url" that your mentioned in $http url params is wrong. Make sure that given url correct or not.

 app.controller("search",function($scope,$http){
                $scope.doSearch = function()
                {
                    $http({
                        method : 'GET',
                        url : 'http://localhost:8080/getHelloWord'
                    }).success(function(data,status,headers,config){
                        alert(data);
                    }).error(function(data,status,headers,config){
                        alert("Request Error");
                    });
                };

});

Can you change the url like what i mentioned above.

By using POSTMAN(RESTclient- Extension of chrome) you can verify the GET request is working fine or not.

Sign up to request clarification or add additional context in comments.

1 Comment

Nope. The URL used by OP is correct. He's just using a relative URL instead of an absolute URL like you mentioned here
0

I solve the problem. I need to add in web.xml

<servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

And create a new file dispatcher-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.siemens.cvc.isrules.controller"  />
    <mvc:annotation-driven />
</beans>

And now works fine. Thx all for support.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.