2

I have some huge trouble receiving JSON from my simple Spring Controller although I checked against many other tutorials and even the official spring blog and could not find any difference, so please help me.

So my dependencies in my project are:

  • spring-context 3.2.2 RELEASE
  • spring-web 3.2.2 RELEASE
  • spring-webmvc 3.2.2 RELEASE
  • spring-test 3.2.2 RELEASE
  • junit 4.10
  • servlet-api 2.5
  • atmosphere-runtime 1.1.0 RC4
  • logback-classic 1.0.13
  • libthrift 0.9.0
  • jackson-mapper-asl 1.9.12
  • jackson-core-asl 1.9.12

My Controller is very simple and just generates a random UUID and returns it. It looks as follows:

@Controller
public class SimpleController {

@RequestMapping(value="/new", method=RequestMethod.GET)
public @ResponseBody SimpleResponse new() throws JsonGenerationException, JsonMappingException, IOException {

    SimpleResponse sr = new SimpleResponse();
    sr.setId(UUID.randomUUID().toString());

    return sr;

    }
}

The model is just a simple POJO like

public class SimpleResponse {

private String id;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

}

Configuration is done like this

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">

<display-name>SimpleTest</display-name>

<servlet>
    <servlet-name>app</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>app</servlet-name>
    <url-pattern>/app/*</url-pattern>
</servlet-mapping>

</web-app>

and

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

<mvc:annotation-driven />
<context:component-scan base-package="de.tum.ibis.wsc" />

</beans>

So thats the server side. On the client side I have a html page with just one line of jQuery code

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
    <script src="http://code.jquery.com/jquery.js"></script>
    <script>

        $(document).ready(function() {

            $.getJSON("http://localhost:8080/Frontend/app/new", function(data) { console.log("it works"); });

        });

    </script>

</head>
<body>

</body>
</html>

Now according to everything I have read this should work but it does not for me. If I call localhost:8080/Frontend/app/new directly in my browser I get something like this: {"id":"b46b8d67-5614-44ed-90ef-d2da14d260f6"} and Firebug tells me that the response header from the server is

HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Server: Jetty(7.6.5.v20120716)

so content-type should be fine. Well if I now run the jquery ajax call I get the error "JSON.parse: unexpected end of data " in jquery.js and I have no cloue why. I hope anybody can help me with that. Thanks!

------ Update ------

Firebug: jQuery error

Firebug: jQuery error

Firebug: All I get

Firebug: All I get

Firebug: This is what I get if a access the url directly

Firebug: This is what I get if a access the url directly

6
  • 1
    Inspect the ajax request with your firebug and see the response text Commented Jun 26, 2013 at 10:43
  • I added three pictures of all that I get Commented Jun 26, 2013 at 10:53
  • I notice that in your request (direct access) you are accepting text/html (which is not correct), while your JSON request is asking for application/json. You should try to use a REST client (Firefox has an addon for it I think) and see if that's causing the problem or not. I think that your Spring controller is only mapped on text/html requests. Commented Jun 26, 2013 at 11:40
  • @DimitriM I just tried to access the URL with the REST Client plugin for firefox and it also worked just fine Commented Jun 26, 2013 at 11:51
  • Did you define a header Accept: application/json? Commented Jun 26, 2013 at 11:54

2 Answers 2

2

Try configuring ContentNegotiationManagerFactoryBean in Spring XML config, see Spring docs

Set favorPathExtension to false and update method's @RequestMapping like so

@RequestMapping(value="/new", method=RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer but did not have any effect :(
@Ralf just a wild guess - HTML page where you have your jQuery code, is it servered from within your application (i.e. is it on the same domain - localhost)? Origin: null in Firebug data looks suspicious.
Oh my god!!! I did the dumbest thing one can do. Somehow I was not thinking and my brain was totally turned of. I tried to call the controller running on localhost from a html page located on my filesystem so file:///. If I put the html page on the server and run the ajax call it works just fine. Shame on me. Thank you!! :)
0

In your AJAX request, you're using

http://localhost:8080/Frontend/app/new

And your servlet declares URL "/new", you should use "/app/new" instead.

@RequestMapping(value="/app/new", method=RequestMethod.GET)

1 Comment

Thansk for the answer but the /app specifies the spring dispatcher servlet as seen in web.xml <url-pattern>/app/*</url-pattern>. So that cannot cause the problem and I am able to access it directly by calling the url so the request dispatching is correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.