从Spring Controller中的文件(JSONObject)返回模拟JSON

问题描述 投票:3回答:5

我想模拟一些JSON(我正在从文件中读取),并返回它作为一些Spring Controller的结果。

文件当然包含正确的JSON数据格式,如:

{"country":"","city":""...}

我的控制器看起来像:

@RestController
@RequestMapping("/test")
public class TestController {

    @Value("classpath:/META-INF/json/test.json")
    private Resource testMockup;

    @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody JSONObject getTest() throws IOException {
        JSONObject jsonObject = new JSONObject(FileUtils.readFileToString(testMockup.getFile(), CharEncoding.UTF_8));
        return jsonObject;
    }
}

读取文件本身没有问题等.jsonObject本身,从debbuging PoV是正确的,但是我从浏览器获得HTTP状态406。我也尝试过返回String(通过返回jsonObject.toString()),而不是JSONObject。但是它会导致编码问题 - 所以来自浏览器的JSON不是JSON本身(一些额外的斜杠,引号等)。

有没有办法,从文件中返回JSON?

java json spring controller jsonobject
5个回答
2
投票
@Controller
public class TestController {

    @RequestMapping(
      value = "/test", 
      method = RequestMethod.GET, 
      produces = MediaType.APPLICATION_JSON_VALUE
    )

    String getTest() {
        return "json/test.json";
    }
}

这对我有用。

JSON文件的路径:\src\main\resources\static\json\test.json


2
投票

这对我有用。

Java的:

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;

@RestController("/beers")
public class BeersController {

    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody
    Object getBeers() {
        Resource resource = new ClassPathResource("/static/json/beers.json");
        try {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.readValue(resource.getInputStream(), Object.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}

Kotklin:

import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.core.io.ClassPathResource
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/beers")
class BeersController {

    @GetMapping
    fun getBeers(): Any? {
        val resource = ClassPathResource("/static/json/beers.json")
        return ObjectMapper().readValue(resource.inputStream, Any::class.java)
    }

}

0
投票

那不是有效的JSON。如果它不是拼写错误,请尝试重新格式化您的文件

{"country":"","city":""}

请注意属性名称周围的开头引号。


0
投票

你和杰克森一起试过吗?

ObjectMapper mapper = new ObjectMapper();
Object json = mapper.readValue(input, Object.class);
String s = mapper.writeValueAsString(json);

也许直接把它写进回应体?杰克逊应该照顾json。


-1
投票

我知道我为此已经很晚了,但你为什么不尝试下面的解决方法。

@RequestMapping(method = RequestMethod.GET)
public @ResponseBody String getTest() throws IOException {
    JSONObject jsonObject = new JSONObject(FileUtils.readFileToString(testMockup.getFile(), CharEncoding.UTF_8));
    return jsonObject.toString();
}
© www.soinside.com 2019 - 2024. All rights reserved.