문제

I'm currently trying to build a REST webservice using Java/EE(5) that fully encapsulates the http responses, so every request should give back response code 200 (OK) and should look like this:

{   
    "msg" : { // imagine some datastructure here },
    "error" : {
        "code" : 200 // http response code
        "status" : "OK" // some string defining this
    }
}

My prefered framework is JAX-RS (we plan to migrate to EE6 soon, so migration is one of the topics while developing this), but can JAX-RS do this?

도움이 되었습니까?

해결책

The easiest way to always return 200 OK and Content-Type: application/json with JAX-RS:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

@Path("/not-rest")
@Produces("application/json")
public class NotRestBean {

    @GET
    public Response getSoapStyle() {
        String json = "{}"; // build your response here
        return Response.ok(json).build();
    }
}

Again, I don't recommend to do this. A central part of REST is the Uniform Interface which includes proper response codes.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top