Search in sources :

Example 1 with JAXBContextFactory

use of feign.jaxb.JAXBContextFactory in project nutzboot by nutzam.

the class FeignStarter method getEncoder.

protected Encoder getEncoder(FeignInject fc, Field field) {
    String encoderStr = Strings.sBlank(fc.encoder(), conf.get(PROP_ENCODER, ""));
    switch(encoderStr) {
        case "":
        case "raw":
            break;
        case "nutzjson":
            JsonFormat jf = JsonFormat.full();
            String jfStr = Strings.sBlank(fc.jsonFormat(), conf.get(PROP_JSON_FORMAT, ""));
            if (!Strings.isBlank(jfStr)) {
                if (jfStr.startsWith("{")) {
                    jf = Json.fromJson(JsonFormat.class, jfStr);
                } else if (jfStr.startsWith("ioc:")) {
                    jf = ioc.get(JsonFormat.class, jfStr.substring(4));
                } else {
                    try {
                        jf = (JsonFormat) JsonFormat.class.getMethod(jfStr).invoke(null);
                    } catch (Exception e) {
                        log.infof("invaild JsonFormat=[%s] at %s", jfStr, field);
                    }
                }
            }
            return new NutzJsonEncoder(jf);
        case "jackson":
            return new JacksonEncoder();
        case "gson":
            return new GsonEncoder();
        case "jaxb":
            JAXBContextFactory jaxbFactory = new JAXBContextFactory.Builder().withMarshallerJAXBEncoding("UTF-8").withMarshallerSchemaLocation(getSchemaString(fc.schema())).build();
            return new JAXBEncoder(jaxbFactory);
        default:
            if (encoderStr.startsWith("ioc"))
                return ioc.get(Encoder.class, encoderStr.substring(4));
            break;
    }
    return null;
}
Also used : JAXBContextFactory(feign.jaxb.JAXBContextFactory) JsonFormat(org.nutz.json.JsonFormat) GsonEncoder(feign.gson.GsonEncoder) JAXBEncoder(feign.jaxb.JAXBEncoder) Encoder(feign.codec.Encoder) JacksonEncoder(feign.jackson.JacksonEncoder) GsonEncoder(feign.gson.GsonEncoder) JAXBEncoder(feign.jaxb.JAXBEncoder) JacksonEncoder(feign.jackson.JacksonEncoder)

Example 2 with JAXBContextFactory

use of feign.jaxb.JAXBContextFactory in project feign by OpenFeign.

the class SOAPCodecTest method encodesSoapWithCustomJAXBNoSchemaLocation.

@Test
public void encodesSoapWithCustomJAXBNoSchemaLocation() {
    JAXBContextFactory jaxbContextFactory = new JAXBContextFactory.Builder().withMarshallerNoNamespaceSchemaLocation("http://apihost/schema.xsd").build();
    Encoder encoder = new SOAPEncoder(jaxbContextFactory);
    GetPrice mock = new GetPrice();
    mock.item = new Item();
    mock.item.value = "Apples";
    RequestTemplate template = new RequestTemplate();
    encoder.encode(mock, GetPrice.class, template);
    assertThat(template).hasBody("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<SOAP-ENV:Header/>" + "<SOAP-ENV:Body>" + "<GetPrice xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://apihost/schema.xsd\">" + "<Item>Apples</Item>" + "</GetPrice>" + "</SOAP-ENV:Body>" + "</SOAP-ENV:Envelope>");
}
Also used : JAXBContextFactory(feign.jaxb.JAXBContextFactory) Encoder(feign.codec.Encoder) RequestTemplate(feign.RequestTemplate) Test(org.junit.Test)

Example 3 with JAXBContextFactory

use of feign.jaxb.JAXBContextFactory in project feign by OpenFeign.

the class SOAPCodecTest method encodesSoapWithCustomJAXBSchemaLocation.

@Test
public void encodesSoapWithCustomJAXBSchemaLocation() {
    JAXBContextFactory jaxbContextFactory = new JAXBContextFactory.Builder().withMarshallerSchemaLocation("http://apihost http://apihost/schema.xsd").build();
    Encoder encoder = new SOAPEncoder(jaxbContextFactory);
    GetPrice mock = new GetPrice();
    mock.item = new Item();
    mock.item.value = "Apples";
    RequestTemplate template = new RequestTemplate();
    encoder.encode(mock, GetPrice.class, template);
    assertThat(template).hasBody("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<SOAP-ENV:Header/>" + "<SOAP-ENV:Body>" + "<GetPrice xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://apihost http://apihost/schema.xsd\">" + "<Item>Apples</Item>" + "</GetPrice>" + "</SOAP-ENV:Body>" + "</SOAP-ENV:Envelope>");
}
Also used : JAXBContextFactory(feign.jaxb.JAXBContextFactory) Encoder(feign.codec.Encoder) RequestTemplate(feign.RequestTemplate) Test(org.junit.Test)

Example 4 with JAXBContextFactory

use of feign.jaxb.JAXBContextFactory in project feign by OpenFeign.

the class SOAPCodecTest method encodesSoapWithCustomJAXBMarshallerEncoding.

@Test
public void encodesSoapWithCustomJAXBMarshallerEncoding() {
    JAXBContextFactory jaxbContextFactory = new JAXBContextFactory.Builder().withMarshallerJAXBEncoding("UTF-16").build();
    Encoder encoder = new SOAPEncoder.Builder().withJAXBContextFactory(jaxbContextFactory).withCharsetEncoding(StandardCharsets.UTF_16).build();
    GetPrice mock = new GetPrice();
    mock.item = new Item();
    mock.item.value = "Apples";
    RequestTemplate template = new RequestTemplate();
    encoder.encode(mock, GetPrice.class, template);
    String soapEnvelop = "<?xml version=\"1.0\" encoding=\"UTF-16\" ?>" + "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<SOAP-ENV:Header/>" + "<SOAP-ENV:Body>" + "<GetPrice>" + "<Item>Apples</Item>" + "</GetPrice>" + "</SOAP-ENV:Body>" + "</SOAP-ENV:Envelope>";
    byte[] utf16Bytes = soapEnvelop.getBytes(StandardCharsets.UTF_16LE);
    assertThat(template).hasBody(utf16Bytes);
}
Also used : JAXBContextFactory(feign.jaxb.JAXBContextFactory) Encoder(feign.codec.Encoder) RequestTemplate(feign.RequestTemplate) Test(org.junit.Test)

Example 5 with JAXBContextFactory

use of feign.jaxb.JAXBContextFactory in project feign by OpenFeign.

the class SOAPCodecTest method decodeAnnotatedParameterizedTypes.

@Test
public void decodeAnnotatedParameterizedTypes() throws Exception {
    JAXBContextFactory jaxbContextFactory = new JAXBContextFactory.Builder().withMarshallerFormattedOutput(true).build();
    Encoder encoder = new SOAPEncoder(jaxbContextFactory);
    Box<String> boxStr = new Box<>();
    boxStr.set("hello");
    Box<Box<String>> boxBoxStr = new Box<>();
    boxBoxStr.set(boxStr);
    RequestTemplate template = new RequestTemplate();
    encoder.encode(boxBoxStr, Box.class, template);
    Response response = Response.builder().status(200).reason("OK").request(Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8)).headers(Collections.emptyMap()).body(template.body()).build();
    new SOAPDecoder(new JAXBContextFactory.Builder().build()).decode(response, Box.class);
}
Also used : Response(feign.Response) JAXBContextFactory(feign.jaxb.JAXBContextFactory) Encoder(feign.codec.Encoder) RequestTemplate(feign.RequestTemplate) Test(org.junit.Test)

Aggregations

Encoder (feign.codec.Encoder)5 JAXBContextFactory (feign.jaxb.JAXBContextFactory)5 RequestTemplate (feign.RequestTemplate)4 Test (org.junit.Test)4 Response (feign.Response)1 GsonEncoder (feign.gson.GsonEncoder)1 JacksonEncoder (feign.jackson.JacksonEncoder)1 JAXBEncoder (feign.jaxb.JAXBEncoder)1 JsonFormat (org.nutz.json.JsonFormat)1