0

In a restful project, I'm trying to use a generic response. In this generic response, there is a static responseBuilder. But the build method in responseBuilder cannot accept generic type. Code :

public class RestResponse<T>{

private int status;

private String message;

private T entity;

/**
 * 
 */
public static class RestResponseBuilder {

    private final RestResponse restResponse;

    public RestResponseBuilder(RestResponse resp) {
        this.restResponse = resp;
    }

    public static RestResponseBuilder ok() {

        return status(HttpServletResponse.SC_OK).msg("ok");
    }

    public static RestResponseBuilder status(int status) {
        final RestResponse resp = new RestResponse();
        resp.setStatus(status);

        return new RestResponseBuilder(resp);
    }

    public RestResponseBuilder msg(String msg) {
        this.restResponse.setMessage(msg);
        return this;
    }

    public RestResponseBuilder entity(Object entity) {
        this.restResponse.setEntity(entity);
        return this;
    }

    public RestResponse build() {

        return restResponse;
    }
}

}

When I use like this : RestResponseBuilder.ok().entity(null).build(); There a warning : Type safety: The expression of type RestResponse needs unchecked conversion to conform to

My question is, how can I add generic type in RestResponseBuilder to avoid this warning? Thanks

1
  • Why not just make RestResponseBuilder generic as well, using the same type parameter? (class RestResponseBuilder<T> {, public RestResponseBuilder(RestResponse<T> resp) {, etc.)? Commented Nov 17, 2018 at 11:50

1 Answer 1

1

Don't use raw types. Make your builder class generic, too, and its static methods generic, too:

public class RestResponse<T> {

    private int status;

    private String message;

    private T entity;

    /**
     *
     */
    public static class RestResponseBuilder<T> {

        private final RestResponse<T> restResponse;

        public RestResponseBuilder(RestResponse<T> resp) {
            this.restResponse = resp;
        }

        public static <T> RestResponseBuilder<T> ok() {

            return RestResponseBuilder.<T>status(200).msg("ok");
        }

        public static <T> RestResponseBuilder<T> status(int status) {
            final RestResponse<T> resp = new RestResponse<T>();
            resp.status = status;

            return new RestResponseBuilder<T>(resp);
        }

        public RestResponseBuilder<T> msg(String msg) {
            this.restResponse.message = msg;
            return this;
        }

        public RestResponseBuilder<T> entity(T entity) {
            this.restResponse.entity = entity;
            return this;
        }

        public RestResponse<T> build() {

            return restResponse;
        }
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.