开发者

What is the best way to get a json object from the web controller?

I am doing a web site using spring mvc framework. I want to do a call ajax and receive a json object directly from my web controller. What is the best way/manner/plugin to do that? i have seen the tutorials from keith donald but, it as think a bit complicated. thanks for any advise.

i have put the jakson json mapper in my pom file. my controller looks like :

    @RequestMapping(value = "m015", method = RequestMethod.GET)
public @ResponseBody String list(@RequestParam(value = "type", required = true) String type){
    List<Mail> mails = mailService.getUserMails((Long) WebHelper.getPrincipal().getUser().getId(),type);
    logger.info("yeah");
    return mails.toString();
}   

but i get this my javascript : [com.stunaz.domain.Mail@94e0a6a2]

my ajax call looks like :

var xhrArgs = {
url: "${ctx}/portal/mail/m015.do",
content: { type: id }, 
headers: {'Content-Type':'application/json'},
handleAs: 't开发者_JS百科ext',
sync: true,
load: function(data) {
    alert(data);
},

}; dojo.xhrGet(xhrArgs);

What did i miss please?


In spring 3.0 it is as simple as:

@RequestMapping("/ajax/foo")
@ResponseBody
public Foo foo(){
    return new Foo();
}

Make sure that you have a the jackson json library in your app and spring will serialise the result as json automagically.


The way described in the blog is actually very simple and elegant. And you can reuse the controller method to get the response in JSON, XML etc...

What you do is you simply annotate the method with @ResponseBody and include a JSON jar in your project. I myself use maven and do this by adding this dependency:

<!-- Jackson JSON Mapper -->
<dependency>
 <groupId>org.codehaus.jackson</groupId>
 <artifactId>jackson-mapper-asl</artifactId>
 <version>1.5.4</version>
</dependency>

The beauty in this is then that the controller method can return a POJO (preferrably a DTO) and the mapper will map this to JSON, as long as the request to the server has the right headers with accept-encoding: application/json (or whatever it was). It even translates incoming requests with the right content-type to the dto type in the method signature. Beautiful!

Example of a method:

@RequestMapping(value ="/service/status", method = RequestMethod.GET)
public @ResponseBody Status getServiceStatus() {

    return new Status("Online", 60, "Nothing to report");
}

Using this approach you can reuse your methods (I use this for a REST api) for JSON, Web Services etc...


<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="order" value="0" />
    <property name="interceptors">
        <list>
            <ref bean="localeChangeInterceptor" />
        </list>
    </property>
</bean>

<!-- Enables annotated POJO @Controllers -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
    <property name="messageConverters">
        <list>
            <ref bean="jsonConverter" />
        </list>
    </property> 
</bean>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜