tillias
tillias

Reputation: 1275

@RestController cannot convert from ResponseEntity<Void>

I've got following code for my REST Controller:

    @RequestMapping(value = "foo", method = RequestMethod.GET)
public ResponseEntity<Result> doSomething(@RequestParam int someParam)
{
    try
    {
        final Result result = service.getByParam(someParam);
        if (result == null)
        {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        } else
        {
            return ResponseEntity.ok(result);
        }

    } catch (Exception ex)
    {
        LOG.error("Error blah", ex);
        return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

I would like to use ResponseEntity.noContent().build() but Eclipse gives me:

Type mismatch: cannot convert from ResponseEntity to ResponseEntity

Is there any way to overcome this?

Update:

It is possible to create helper like this:

public class ResponseUtils
{
    public static <T> ResponseEntity<T> noContent()
    {
        return withStatus(HttpStatus.NO_CONTENT);
    }

    public static <T> ResponseEntity<T> internalServerError()
    {
        return withStatus(HttpStatus.INTERNAL_SERVER_ERROR);
    }

    public static <T> ResponseEntity<T> accepted()
    {
        return withStatus(HttpStatus.ACCEPTED);
    }

    private static <T> ResponseEntity<T> withStatus(HttpStatus status)
    {
        return new ResponseEntity<T>(status);
    }
}

So I can use it like:

return ResponseUtils.noContent();

But maybe there is built-in functionality for this stuff?

Upvotes: 0

Views: 8412

Answers (1)

m.aibin
m.aibin

Reputation: 3593

Is this that you want to achieve?

@RequestMapping(value = "foo", method = RequestMethod.GET)
public ResponseEntity<Result> doSomething(@RequestParam int someParam) {
    try {
        final Result result = service.getByParam(someParam);
        if (result == null) {
            return ResponseUtils.noContent();
        } else {
            return new ResponseEntity<Result>(result, null, HttpStatus.ACCEPTED);
        }

        } catch (Exception ex) {
            return ResponseUtils.internalServerError();
        }
}

//you forgot to add static keyword in this Utils class
public static class ResponseUtils{
    public static <T> ResponseEntity<T> noContent(){
       return withStatus(HttpStatus.NO_CONTENT);
    }

    public static <T> ResponseEntity<T> internalServerError(){
       return withStatus(HttpStatus.INTERNAL_SERVER_ERROR);
    }

    public static <T> ResponseEntity<T> accepted(){
       return withStatus(HttpStatus.ACCEPTED);
    }

    private static <T> ResponseEntity<T> withStatus(HttpStatus status){
       return new ResponseEntity<T>(status);
    }
}

Check imports, I am using:

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.xml.transform.Result;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

Upvotes: 1

Related Questions