使用Apache CXF的JAX-RS Client发送HTTP请求

技术公众号:Java In Mind(Java_In_Mind),欢迎关注!

Apache CXF JAX-RS Client介绍

JAX-RS Client是Apache CXF的基于JAX-RS规范的客户端实现,Apache CXF是一个开放源代码服务框架, CXF使用Front-End API(例如JAX-WS和JAX-RS)来构建和开发服务。 这些服务可以说各种协议,例如SOAP,XML / HTTP,RESTful HTTP或CORBA,并且可以通过各种传输方式(例如HTTP,JMS或JBI)工作。

使用Apache CXF可以和Spring很方便的集成,也可以单独使用,这里我们只使用JAX-RS Client模块来实现RESTFul API的调用,他可以很好地提供面向对象编程的实现,并且支持各种数据封装,很方便我们做代码层面上的抽象与封装。

依赖

a
    org.apache.cxf
    cxf-rt-rs-client
    3.0.15



    com.fasterxml.jackson.jaxrs
    jackson-jaxrs-json-provider
    2.8.10

模拟服务

@RestController
@RequestMapping("/demo")
@SpringBootApplication
@Slf4j
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    private List foos = new CopyOnWriteArrayList<>();

    @GetMapping("/list")
    public ResponseEntity list(@RequestParam(value = "name", required = false) String name) {
        log.info("accept a list request...");
        boolean emptyQuery = StringUtils.isEmpty(name);
        return ResponseEntity
            .ok(foos.stream().filter(i -> emptyQuery || i.getName().equals(name)).collect(Collectors.toList()));
    }

    @PostMapping
    public ResponseEntity create(@RequestBody Foo foo) {
        log.info("accept create request,foo:{}", foo);
        // uuid
        foo.setId(UUID.randomUUID().toString());
        // add
        foos.add(foo);
        return ResponseEntity.ok(foo);
    }

    @GetMapping("/error")
    public ResponseEntity error() {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("error");
    }

    @Data
    @AllArgsConstructor
    public static class Foo {
        private String id;
        private String name;
        private int age;
    }
}


序列化Provider

public JacksonJaxbJsonProvider jsonProvider() {
        JacksonJaxbJsonProvider jacksonJaxbJsonProvider = new JacksonJaxbJsonProvider();
        Jackson2ObjectMapperFactoryBean jackson2ObjectMapperFactoryBean = new Jackson2ObjectMapperFactoryBean();
        jackson2ObjectMapperFactoryBean.afterPropertiesSet();
        ObjectMapper objectMapper = jackson2ObjectMapperFactoryBean.getObject();
        objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        jacksonJaxbJsonProvider.setMapper(objectMapper);
        return jacksonJaxbJsonProvider;
}

发起POST请求

@Data
public class FooCreateDTO {
    private String name;
    private int age;
}

@Data
public class FooDTO {
    private String id;
    private String name;
    private int age;
}

String endPoint = "http://localhost:8080";

FooClient fooClient = JAXRSClientFactory.create(endPoint, FooClient.class, Collections.singletonList(jsonProvider()), true);

FooCreateDTO fooCreateDTO = new FooCreateDTO();
fooCreateDTO.setName("seven");
fooCreateDTO.setAge(18);

FooDTO fooDTO = fooClient.create(fooCreateDTO);

log.info("result:{}",fooDTO);

发起GET请求

String endPoint = "http://localhost:8080";

FooClient fooClient = JAXRSClientFactory.create(endPoint, FooClient.class, Collections.singletonList(jsonProvider()), true);

List list = fooClient.list("seven");
log.info("result:{}",list);

异常处理

String endPoint = "http://localhost:8080";

FooClient fooClient = JAXRSClientFactory.create(endPoint, FooClient.class, Collections.singletonList(jsonProvider()), true);

try{
    String message = fooClient.error();
    System.out.println(message);
}catch (WebApplicationException e){
    Response response = e.getResponse();
    int status = response.getStatus();
    String body = response.readEntity(String.class);

    log.error("error,status code={},body={}",status,body);
}

集成Spring

集成Spring可以带了许多好处,首先是解耦,然后可以配置分离,并且对开发友好,就好像是调用本地服务一样区实现调用一个外部请求这样使用起来方便,对于测试也方便。

@Bean
public FooClient fooClient(){
        //一般在配置文件中配置
        String endPoint = "http://localhost:8080";
        return JAXRSClientFactory.create(endPoint, FooClient.class,         Collections.singletonList(jsonProvider()), true);
}

@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class RunWithSpringTest {

    @Autowired
    private FooClient fooClient;

    @Test
    public void list(){
        List list = fooClient.list(null);
        System.out.println(list);
    }
}

其他配置

  • 超时配置

    FooClient fooClient = ...//get Client
    //set Timeout
    HTTPConduit conduit = WebClient.getConfig(client).getHttpConduit();
    HTTPClientPolicy policy = new HTTPClientPolicy();
    policy.setReceiveTimeout(millisecond);
    conduit.setClient(policy);
    
  • 统一Header

    FooClient fooClient = ...//get Client
    //set token
    WebClient.client(client).header("access-token", "Bearer " + token);
    

总结

使用JAX-RS Client来调用RESTFul API更加方便了,代码也更符合面向对象编程,并且可以方便地集成到Spring框架中,它帮我们处理了很多事情,请求体的构造、结果的封装等等,使我们更加专注于业务逻辑的编写就够了,避免繁琐的重复类似的代码处理。

你可能感兴趣的:(使用Apache CXF的JAX-RS Client发送HTTP请求)