2

I have three or more types of request that come in form of JSON data and convert to object.

Let's say that there is a request:

{
  "id":1,
  "type":"0",
  "url":
  "http://stackoverflow.com"
}

And I want to build some sort of conncetion factory that allows me to get pages like that:

Request request = new Request(json);
Response response = request.execute();

Of course the execute method has different implementation for each of the request's types.

I have a prototype that is written in plain JavaSE and I want to migrate to Spring Framework and also get some feedback about my code if it has problems. First of all I built a SimpleRequest that just obtains the page and returns the body.

class SimpleRequest implements Request{
    Package requestData;
    @Override
    public Connection.Response execute() throws IOException {
        Connection.Response response = Jsoup
                                           .connect(requestData.getUrl())
                                           .execute();
        return response;
    }

    public void bind(Package requestData){
        this.requestData = requestData;
    }
}

This method just fetch the page. Nothing interesting.

Also I have RequestTemplate that prepare request:

class RequestTemplate{
    Package requestData;
    RequestFactory requestFactory = new RequestFactory();
    Request request;
    public RequestTemplate(Package requestData){
        this.requestData = requestData;
        try {
            request = requestFactory.getRequestInstance(requestData.getType());
            request.bind(requestData);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public Connection.Response execute(){
        Connection.Response connection = null;
        try{
            connection = request.execute();
        }catch (Exception e){
            e.printStackTrace();
        }
        return connection;
    }
}

This method obtains an object based of RequestType;

And of course I have RequestFactory that has a description of objects and create new Instances of them.

class RequestFactory{
    public Request getRequestInstance(RequestType type) throws Exception {
        switch (type){
            case SIMPLE:
                return new SimpleRequest();
            default:
                throw new Exception("Error");
        }
    }}

So we check the RequestType type matching with the enum list and return new instance.

I also what to implement something like that in my small project that are based on Spring Framework and there are two issues.

  1. As I understand the code above is a bit messy and has some performance problems. I think that I'll use the code to serve lots of connection so that I have to optimize it somehow because of memory limit and so on.

  2. Integration with Spring. It's a framework so I think there should be some features that could make my life easier. I've read about FactoryBean, but haven't caught how to wire it with my task. As I mentioned above that the code is a bit messy and i think that i created to much abstraction.

Thanks.

Ascelhem
  • 413
  • 3
  • 21
  • In what sense is this more desirable then, say, a @Controller which does all the binding, possibly with your own DataBinder so that your controller method then becomes `public ExecuteResponse handleMyRequest(FirstRequest req)`. See http://stackoverflow.com/questions/6519322/spring-mvc-customized-method-parameter-binding – Koos Gadellaa Sep 05 '16 at 13:42
  • @KoosGadellaa, well, i think that I didn't understand something. There is no problem with serializing the json and bind it. I use gson to manage the task. The problem is connected with factory and template...And I also think that `controller` annotation is only Web app, but my application is a standalone application without rest. – Ascelhem Sep 05 '16 at 13:51
  • 1
    As a client goes, have you looked at Spring's RestTemplate http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html and registering some additional `HttpMessageConverter`s to build your response? – Koos Gadellaa Sep 05 '16 at 14:56

0 Answers0