Home > other >  Rest api, adding another MediaType, default response
Rest api, adding another MediaType, default response

Time:01-28

I have REST api in Spring MVC that is used commonly by many users. Let's say I have endpoint like that:

@GET
@Path("/getNumber")
@Produces({ MediaType.TEXT_PLAIN })
public String getNumber(Long id) {
    return service.getNumber(id);
}

Now I would like to add another media type, APPLICATION_JSON, to annotation @Produces. May that cause some problems with user apps that are using this endpoint? For example when they are not specyfing header with response type and are expecting response message will be text, may this change cause that they will get response in json and in result integration with this api will fail?

CodePudding user response:

Content negotiation strategy can be used for this .Please refer this https://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc

CodePudding user response:

As a best practice common used Media-Type is XML or Json, but mostly Json because it is less verbose than XML. I suggest you this post. Even if you want to set a default type, it should be Json.

  •  Tags:  
  • Related