0

I want to send a number (Java reference type Double) in my ResponseEntity:

@Controller
public class ProjectController {

    @RequestMapping(value="/get/number")
    public ResponseEntity<Double> getNumber(){

        Double resp = new Double(654321.5432d);
        ResponseEntity<Double> respEntity = new ResponseEntity<Double>(resp, HttpStatus.OK); 
        return respEntity;

    }
}

In UI side, I am using AngularJS:

$scope.getNumber = function(){
    DataNumber.getData({

        },function(data){
                console.log("Successfully Called get data number");
                $scope.numberData = data;
                $scope.showNumber = true;
        },
        function(){
            console.log("Error Happened!");
    });
};

The final output is like this:

{"0":"6","1":"5","2":"4","3":"3","4":"2","5":"1","6":".","7":"5","8":"4","9":"3","10":"2"} 

I tried to see what received in browser, using javascript console, but the response is empty! As following image shows:

enter image description here

As you can see the Double is transformed to a JSON object! Each digit is now a field of a JSON object!

My Questions:

  • If we want to return a number as a response using SpringMVC, what is the standard way?

  • If the way I did it is correct, then:

    • how should I transfer this JSON object to a single number without coding! (I now how to write the code and get produce the number, I am looking for probable existing functions to do so.)

    • Why browser shows nothing in response!

Thanks in advance.

SamRaxin
  • 1
  • 2
  • I replicate the problem with Apache CXF. So I think, it is not problem in Web-service side. I think AngularJS could not interpret the Double returned from REST service – SamRaxin Jan 11 '16 at 04:21

1 Answers1

1

Try this

@RequestMapping(value="/get/number")
public @ResponseBody Double getNumber(){

    Double resp = new Double(654321.5432d);

    return resp;

} 

It will return a double value..

morewater
  • 67
  • 8
  • [SpringMVCDoc](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html) there is some different with @ResponseBody and use HttpEntity to response – morewater Dec 18 '15 at 10:17