春休文档.生成代码段时无效的 UTF-8 中间字节



请帮助我解决我在使用 Spring Rest 文档时遇到的问题。我已经从 Spring 手册中进行了所有必需的设置。我写了Spring MVC测试。这是代码。奇怪的符号是俄语。

@WebAppConfiguration
@ContextConfiguration(classes = {TestConfiguration.class})
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles({"test"})
@Slf4j
public class ProductApiControllerTest {
    protected static final long TEST_PRODUCT_ID_1 = 11_071_076_993L;
    protected static final long TEST_PRODUCT_ID_2 = 21_071_076_994L;
    public static final Product TEST_PRODUCT_1 = Product.builder()
            .id(TEST_PRODUCT_ID_1)
            .productId(TEST_PRODUCT_ID_1)
            .name("WRX3000")
            .fullName("WRX3000")
            .regionPickupAvailable(Arrays.asList("a100"))
            .regionDeliveryAvailable(Arrays.asList("b200"))
            .categoryId(100500)
            .categoryName("Телевизоры")
            .categories(Arrays.asList(10L, 20L, 30L))
            .brandId(200300)
            .brandName("Samsung")
            .description("AMOLED HD Телевизор 4 поколения")
            .prices(Arrays.asList(Price.builder().baseStore("b200").priceId(777).priceValue(2990d).oldPriceValue(3490d).build()))
            .build();
    @Mock
    private SearchService searchServiceMock;
    @InjectMocks
    private ProductApiController controller;
    private MockMvc mockMvc;
    private RestDocumentationResultHandler document;

    @Rule
    public final RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets");
    @Before
    public void init() {
        MockitoAnnotations.initMocks(this);
        this.document = document("{method-name}", preprocessResponse(prettyPrint()));
        mockMvc = MockMvcBuilders.standaloneSetup(controller)
                .apply(documentationConfiguration(this.restDocumentation).snippets().withEncoding("UTF-8"))
                .alwaysDo(document)
                .build();
    }
    @Test
    public void testGetProduct() throws Exception {
        log.info("Test getProduct(..) from ProductApiController");
        when(searchServiceMock.findByProductId(TEST_PRODUCT_ID_1, "b200")).thenReturn(TEST_PRODUCT_1);
        this.document.snippets(responseFields(
                fieldWithPath("id").description("Идентификатор для служебных целей поисковой машины."),
                fieldWithPath("name").description("Название продукта"),
                fieldWithPath("fullName").description("Полное название продукта"),
                fieldWithPath("productId").description("Идентификатор (SKU) товара"),
                fieldWithPath("regionPickupAvailable").description("Показывает доступен ли продукт для самовывоза в данном регионе"),
                fieldWithPath("regionDeliveryAvailable").description("Показывает доступен ли продукт для доставки в данном регионе"),
                fieldWithPath("categoryId").description("Идентификатор категории, к которой принадлежит товар"),
                fieldWithPath("categoryName").description("Название категории, к которой принадлежит товар"),
                fieldWithPath("categories").description("Список идентификаторов категорий к которым принадлежит товар"),
                fieldWithPath("brandId").description("Идентификатор бренда товара"),
                fieldWithPath("brandName").description("Название бренда продукта"),
                fieldWithPath("description").description("Описание товара"),
                fieldWithPath("propertyAggregate").description("Какая-то фигня"),
                fieldWithPath("propepropertyAggregatertyMap").description("Дополнительные свойства товара"),
                fieldWithPath("price").description("Цена товара"),
                fieldWithPath("oldPrice").description("Предыдущая цена товара")
        ));
        mockMvc.perform(get(Constants.RestApiV1.ROOT_PATH + "/" + Constants.RestApiV1.GET_PRODUCTS + "/" + TEST_PRODUCT_ID_1 + "?baseStore=b200").accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(content().contentType("application/json;charset=UTF-8"))
                .andExpect(jsonPath("$.productId").value(TEST_PRODUCT_ID_1))
                .andExpect(jsonPath("$.name").value("WRX3000"))
                .andExpect(jsonPath("$.fullName").value("WRX3000"))
                .andExpect(jsonPath("$.regionPickupAvailable").value(false)) // a100 != b200
                .andExpect(jsonPath("$.regionDeliveryAvailable").value(true)) // b200 == b200
                .andExpect(jsonPath("$.categoryId").value(100500))
                .andExpect(jsonPath("$.brandName").value("Samsung"));
    }
}

问题是当我从IDE(Intellij IDEA)启动此测试时,它工作正常并生成所有代码片段。但是当我运行 maven"包"任务测试失败时结果:

Tests in error: 
  testGetProduct(ru.eldorado.searchservice.web.controllers.rest.ProductApiControllerTest): com.fasterxml.jackson.core.JsonParseException: Invalid UTF-8 middle byte 0xe5

我已经检查了问题出在字段描述中。如果我删除这部分包目标执行正常。

我的项目和所有输出文件的编码都是 UTF-8。在 maven 中,我明确指定

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

任何帮助和想法将不胜感激。

另一个我的答案只是一个解决方法。真正的问题是俄语版本(Cp-1251)的标准Windows编码。不知何故,maven 参数并没有解决这个问题。但环境可变

JAVA_TOOL_OPTIONS = -Dfile.encoding=UTF8

修复了一切。答案已经在这里找到 如何在 maven 中配置编码

回答我自己的问题。问题出在俄语字符中

 public static final Product TEST_PRODUCT_1 = Product.builder()
            .id(TEST_PRODUCT_ID_1)
            .productId(TEST_PRODUCT_ID_1)
            .name("WRX3000")
            .fullName("WRX3000")
            .regionPickupAvailable(Arrays.asList("a100"))
            .regionDeliveryAvailable(Arrays.asList("b200"))
            .categoryId(100500)
            .categoryName("Телевизоры")
            .categories(Arrays.asList(10L, 20L, 30L))
            .brandId(200300)
            .brandName("Samsung")
            .description("AMOLED HD Телевизор 4 поколения")
            .prices(Arrays.asList(Price.builder().baseStore("b200").priceId(777).priceValue(2990d).oldPriceValue(3490d).build()))
            .build();

我添加了方法

private static String getStringInUtf8(String source) {
    return new String(source.getBytes(StandardCharsets.UTF_8));
}

并使用它来分配声明中包含俄语字符的值TEST_PRODUCT_1

public static final Product TEST_PRODUCT_1 = Product.builder()
        .id(TEST_PRODUCT_ID_1)
        .productId(TEST_PRODUCT_ID_1)
        .name("WRX3000")
        .fullName("WRX3000")
        .regionPickupAvailable(Arrays.asList("a100"))
        .regionDeliveryAvailable(Arrays.asList("b200"))
        .categoryId(100500)
        .categoryName(getStringInUtf8("Телевизоры"))
        .categories(Arrays.asList(10L, 20L, 30L))
        .brandId(200300)
        .brandName("Samsung")
        .description(getStringInUtf8("Super AMOLED HD Телевизор 5 поколения"))
        .prices(Arrays.asList(Price.builder().baseStore("b200").priceId(777).priceValue(2990d).oldPriceValue(3490d).build()))
        .build();

它解决了问题,但尚未找到其根源。

相关内容

最新更新