英文:
How to return json/xml from Spring Boot @Controller?
问题
@Controller
@RequestMapping("/game")
public class ViewController {
    @RequestMapping(value = "/statistic", method = RequestMethod.GET, 
                    produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public @ResponseBody GamerData[] getStatistic() {
        RestTemplate restTemplate = new RestTemplate();
        try {
            String uri_get_statistic = "http://localhost:8081/statistic/";
            ResponseEntity<GamerData[]> response = restTemplate.getForEntity(uri_get_statistic, GamerData[].class);
            GamerData[] statisticData = response.getBody();
            return statisticData;
        } catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
After changing to @Controller, I encountered a 404 error. With @RestController, I was able to receive JSON data. (The GamerData class contains two simple fields: an integer and a string, along with their respective setters, getters, and constructor.)
UPDATE:
I added @ResponseBody to my function, but now I'm facing an Internal Server error:
Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class [Lgame.mainservice.mvc.GamerData;] with preset Content-Type 'null']
<details>
<summary>英文:</summary>
I'm trying to return JSON/XML from a function of my Controller. First I was using @RestController and it worked good, but now I need to change to @Controller, because I will use also some other functions and pass there some objects for my view.
@Controller
@RequestMapping("/game")
public class ViewController {
@RequestMapping(value = "/statistic", method = RequestMethod.GET,
produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public GamerData[] getStatistic() {
RestTemplate restTemplate = new RestTemplate();
try {
String uri_get_statistic = "http://localhost:8081/statistic/";
ResponseEntity<GamerData[]> response = restTemplate.getForEntity(uri_get_statistic, GamerData[].class);
GamerData[] statisticData = response.getBody();
return statisticData;
} catch(Exception e) {
e.printStackTrace();
return null;
}
}
}
After I've changed to Controller I get error 404 not found. With RestController I've got json. (GamerData is just a class with 2 simple fields (int and String), setters, getters, consructor).
UPDATE:
I've added ```@ResponseBody``` to my function, but now I have Internal Server error
```Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class [Lgame.mainservice.mvc.GamerData;] with preset Content-Type 'null']```
</details>
# 答案1
**得分**: 0
尝试像下面这样添加 @ResponseBody:
```java
public @ResponseBody GamerData[] getStatistic....
英文:
try to add @ResponseBody like
public @ResponseBody GamerData[] getStatistic....
答案2
得分: 0
@RequestMapping(value = "/statistic", method = RequestMethod.GET, 
                produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@ResponseBody
public GamerData[] getStatistic() {}
英文:
@RequestMapping(value = "/statistic", method = RequestMethod.GET, 
                produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@ResponseBody  // add this one
public GamerData[] getStatistic() {}
答案3
得分: 0
感谢大家的回答,我已经在我的函数中添加了@ResponceBody注解,并从@RequestMapping注解中移除了produces部分,我的函数运行得很好:
@RequestMapping(value = "/statistic", method = RequestMethod.GET)
@ResponseBody
public GamerData[] getStatistic() {
    //函数代码
}
英文:
Thank you all for answers, I've added @ResponceBody annotation to my function and removed "produces" part from @RequestMapping annotation and my function works great:
@RequestMapping(value = "/statistic", method = RequestMethod.GET)
@ResponseBody
public GamerData[] getStatistic() {
    //function code
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论