简体   繁体   中英

How to pass json to spring without serializing to object?

I'd like to pass json data from javascript to spring boot and get json back in the response. All the examples I've seen require converting (serializing?) the json data to a spring bean and using it instead of the json directly. That seems like an awful lot of needless work and fraught with potential errors.

Is there a way to send the json to a spring controller, the controller passes the json on to the repository where it pulls the inputs it needs for the query, executes the query, returns the result as json to the controller, which returns the json to the javascript?

This seems like a logical and simple way of getting json to and from spring without using Jackson or Gson or some other method to convert things back and forth. If this is possible, can anyone point me to such an example?

The whole point of controllers is to separate the infrastructure level concerns (HTTP status handling and json parsing) from your application concerns (the interaction with the repository).

If you let your repository validate if the json is correct and get the correct query fields, then you will have a hard time maintaining the code, and bugs are in fact more likely to occur.

Yes it is possible. Here is the solution:

Javascript:

var strategyObj = {name:"some-strategy-name",symbol:"SP-500"} //javascript object
var strategyStr = JSON.stringify(strategyObj);  //{"name":"some-strategy-name","symbol":"SP-500"}
var strategyUri = URI.encode(strategyStr); 
$.getJSON("http://localhost:8080/api/v1/backtest?strategy="+strategyUri, function(data) {
                console.log("returned returned data=", data);  //data is json
                strategy = data.parse();  //back to a javascript object

Spring Boot Controller:

public class StrategyController {
    @Autowired
    private StrategyService strategyService;

    @GetMapping("/backtest")
    public Response backtestStrategy(@RequestParam String strategy) {
        Response response = strategyService.backtestStrategy(strategy);
        return response;
    }

Response class:

public class Response {
    public List<Quote> quotes;
    public Strategy strategy;

    public Response() {
        strategy = new Strategy();
    }
//no getters and setters needed
}

StrategyService interface:

public interface StrategyService {
    Response backtestStrategy(String strategy);


StrategyServiceImpl:

    @Override
    public Response backtestStrategy(String strategy) {
        System.out.println("StrategyServiceImpl.backtestStrategy: strategy="+strategy);
        JSONObject jo = new JSONObject(strategy);
        String strategyName = jo.getString("name");
        String symbol = jo.getString("symbol");

    Response response = new Response();
//run backtest
response.value1 = "bla"
response.value2 = "etc"
return response;

Some may say that this is the wrong way to do it. I'll let the data scientists argue it out!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM