通过内容协商进行 MVC 中的版本控制

huangapple go评论78阅读模式
英文:

Versioning through content negotiation in mvc

问题

我有两个使用相同请求路径和响应的 GET API,其中一个已经存在,另一个是新创建的。

@GET
@Path("/{id}/export")
public Response exportVersion1(@PathParam("id") String id, @QueryParam("format") final String format)

@GET
@Path("/{id}/export")
@Consumes({"application/vnd.com.abc.v2+json"})
public Response exportVersion2(@PathParam("id") String id, @QueryParam("format") final String format)

当我将内容类型设置为 application/vnd.com.abc.v2+json 时,我能够调用第二个 API,但如果不提供内容类型,那么也会调用第二个 API。

我想将第一个 API 设置为默认选项,并且不想对其进行更改或添加,因为它与客户端使用的 UI 相关联。我只能对第二个 API 进行更改,请提供任何建议。

英文:

I have two get API with the same request, path, and response out of which one already exists and the other one which is created new.

  @GET
  @Path("/{id}/export")
  public Response exportVersion1(@PathParam("id") String id, @QueryParam("format") final String format)

  @GET
  @Path("/{id}/export")
  @Consumes({"application/vnd.com.abc.v2+json"})
  public Response exportVersion2(@PathParam("id") String id, @QueryParam("format") final String format)

On giving content type as application/vnd.com.abc.v2+json I am able to call the second API but if no content type is given, then also the second one gets called.

I want to make the first one as default and don't want to change/add to it as it is bound to UI used by the client. I can only make changes to the second API, please give any suggestions.

答案1

得分: 1

你可以设置默认内容类型。对于Spring 5,代码如下:

public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.defaultContentType(MediaType.TEXT_PLAIN);
}
英文:

You can set default content type. For spring 5 this will be:

public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.defaultContentType(MediaType.TEXT_PLAIN); 
}

huangapple
  • 本文由 发表于 2020年7月29日 00:18:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63138534.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定