Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
113 views
in Technique[技术] by (71.8m points)

java - Mapping restful ajax requests to spring

I have this piece of code:

@RequestMapping(value = "/test.json", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody Object[] generateFile(@RequestParam String tipo) {
    Object[] variaveis = Variavel.getListVariavelByTipo(tipo);
    return variaveis;
}

As far as I know it should take a request to test.json?tipo=H and return the JSON representation of Variavel[], however when I make such request I get:

HTTP Status 406 -

type Status report

message

descriptionThe resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ()

By using the following function I can get the expected json:

@RequestMapping(value = "/teste.json")
public void testeJson(Model model, @RequestParam String tipo) {
    model.addAttribute("data", Variavel.getListVariavelByTipo("H"));
}

What I'm doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

@RequestBody/@ResponseBody annotations don't use normal view resolvers, they use their own HttpMessageConverters. In order to use these annotations, you should configure these converters in AnnotationMethodHandlerAdapter, as described in the reference (you probably need MappingJacksonHttpMessageConverter).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...