近來在做另一個項目接口設計的時候需要考慮這樣一個需求,一套接口需兼容兩類數據類型(xml和json)。
基於這個項目,原來的接口均為WSDL,遵守的協議為SOAP,它是基於XML的。
於是我想了一些辦法做一些擴展,這樣的擴展保持WSDL不變的前提下,增加少量代碼實現。
由於之前整合Apache CXF用到過,所以很順利的將其復用過來。
核心代碼如下:
@RestController @RequestMapping("/user") public class UserApiController { @PostMapping("/add") public int add(@RequestParam String email, @RequestParam String username, @RequestParam String password) { try { // 接口地址 String address = "http://127.0.0.1:9090/cxf/user?wsdl"; // 代理工廠 JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean(); // 設置代理地址 jaxWsProxyFactoryBean.setAddress(address); // 設置接口類型 jaxWsProxyFactoryBean.setServiceClass(UserService.class); // 創建一個代理接口實現 UserService userService = (UserService) jaxWsProxyFactoryBean.create(); return userService.addUser(email, username, password); } catch (Exception e) { e.printStackTrace(); return -1; } } }
上面是之前整合CXF中的客戶端寫的例子,我在項目中改為如下,減少了內部通信,直接調用service,核心代碼如下:
@RestController @RequestMapping("/user") public class UserApiController { @Autowire private UserService userService; @PostMapping("/add") public int add(@RequestParam String email, @RequestParam String username, @RequestParam String password) { return userService.addUser(email, usern } }
這樣一來,XML和JSON返回數據類型都兼容,同時請求數據類型既可以是JSON,也可以XML,都能很好的兼容。
當然了,如果只對響應數據類型定義,而不用管請求數據是json還是xml,最簡單的辦法就是請求頭定義(核心是Accept)。
如果是已經寫了Controller,原來是JSON數據,現在要求返回XML,還可以這么做,核心配置類如下:
import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; @Configuration public class WebMvcConfig extends WebMvcConfigurationSupport { @Override protected void configureContentNegotiation(ContentNegotiationConfigurer configurer) { configurer.favorPathExtension(true) //是否支持后綴的方式 .parameterName("mediaType") .defaultContentType(MediaType.APPLICATION_JSON) .mediaType("xml", MediaType.APPLICATION_XML) //當后綴名稱為xml的時候返回xml數據 .mediaType("json", MediaType.APPLICATION_JSON);//當后綴名稱是json的時候返回json數據 } }
這樣一來針對響應數據類型,你如果要的是xml只需在請求路徑加上.xml即可,例如
http://localhost:8080/user/list.xml,
直接能獲取xml數據。如果是原來的json數據,路徑不變,例如
http://localhost:8080/user/list
或
http://localhost:8080/user/list.json。