如何从Spring Boot的@Controller返回json/xml?

huangapple go评论69阅读模式
英文:

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&#39;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&#39;ve changed to Controller I get error 404 not found. With RestController I&#39;ve got json. (GamerData is just a class with 2 simple fields (int and String), setters, getters, consructor).

UPDATE:
I&#39;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 &#39;null&#39;]```

</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 = &quot;/statistic&quot;, 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 = &quot;/statistic&quot;, method = RequestMethod.GET)
@ResponseBody
public GamerData[] getStatistic() {
    //function code
}

huangapple
  • 本文由 发表于 2020年9月13日 04:24:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63864638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定