小编gen*_*qew的帖子

HTTP状态424或500表示外部依赖性的错误

我正在尝试创建一个具有2个依赖项的服务.其中一个依赖项是内部管理的,而第二个需要对第三方API进行外部http出站调用.该序列需要更新资源,然后执行http出站呼叫.

所以我的问题是,在第二步失败的情况下,返回正确的http状态代码是什么?

如果响应是424或500,并且消息正文解释了遇到的错误?

  • 424:方法失败 - 表示该方法未在其范围内的特定资源上执行,因为该方法的某些部分执行失败导致整个方法被中止.
  • 500内部服务器错误.

rest http-status-codes

46
推荐指数
3
解决办法
3万
查看次数

通过maven生成JPA Hibernate Metamodel

我按照JPA modelgen 指南,我能够生成我需要的规范元模型.设置这个pom:

<plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArgument>-proc:none</compilerArgument>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.bsc.maven</groupId>
                <artifactId>maven-processor-plugin</artifactId>
                <version>2.0.6-redhat</version>
                <executions>
                    <execution>
                        <id>process</id>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                            <outputDirectory>target/metamodel</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.3</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>target/metamodel</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
Run Code Online (Sandbox Code Playgroud)

生成的源在指定的目录中正确创建,我必须手动将其指定为eclipse项目类路径中的源以使用它.当我触发maven时,日志显示cannot find symbol或者duplicate class我仍然可以成功构建.所以我的问题是,这是创建元模型的预期/正确行为吗?还是我错过了cofig的东西?谢谢

hibernate jpa

14
推荐指数
1
解决办法
1万
查看次数

Java 8 和 Java 11 中 LocalDateTime 到毫秒的差异

我目前正在将一些项目从 Java 8 升级到 Java 11,其中一个转换器的单元测试失败了。基本上问题源于由于先前通过 JDK 8 传递的日期精度导致的相等性检查失败。

这是测试的部分示例,为了清楚起见,我移动了转换器的内容:

@Test
public void testDateTime() {
    LocalDateTime expected = LocalDateTime.now().plusDays(1L);

    // converter contents
    long epochMillis = expected.atZone(ZoneId.systemDefault())
            .toInstant().toEpochMilli();
    LocalDateTime actual = LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMillis),
            TimeZone.getDefault().toZoneId());


    assertThat(actual, equalTo(expected));
}
Run Code Online (Sandbox Code Playgroud)

由于以下原因,这会导致资产错误:

Expected :<2021-06-02T14:06:21.820299>
Actual   :<2021-06-02T14:06:21.820>
Run Code Online (Sandbox Code Playgroud)

我可以用 assertThat(actual, equalTo(expected.truncatedTo(ChronoUnit.MILLIS)))它们相等,但是,这意味着每次与被测试的转换器类进行比较(isAfter、isBefore、equals)时,都必须应用中继。

对于 JDK 11(或者我可能错过的文档:)),是否有正确的方法可以在LocalDateTimeto之间进行转换,Long反之亦然?


更新:

正如评论中所指出的,Java 8 和 11 的表示形式不同,因此导致测试失败。为了提供有关本文所要求内容的更多上下文,这里是测试验证的 2 种方法(我将其移至测试本身以仅捕获正在执行的内容,因为失败的单元测试属于使用实用方法)

public Long localDateTimeToEpochMillis(LocalDateTime ldt) {
    Instant instant = ldt.atZone(ZoneId.systemDefault()).toInstant();
        return ldt.atZone(ZoneId.systemDefault())
           .toInstant().toEpochMilli();
}
Run Code Online (Sandbox Code Playgroud)

public LocalDateTime epochMillisToLocalDateTime(long epochMillis) {
    return LocalDateTime.ofInstant( …
Run Code Online (Sandbox Code Playgroud)

java java-8 java-11 localdatetime

9
推荐指数
1
解决办法
273
查看次数

如何在QueryDSL中选择文字

我目前正在开发一个使用queryDSL和hibernate的项目,其中需要一个select文字.按照这里发布的示例我有:

createQuery().
   from(path()).
      where(specification().getPredicate()).
          list(
   ConstructorExpression.create(Foo.class, Expressions.constant(BigDecimal.ONE)));
Run Code Online (Sandbox Code Playgroud)

其中Foo类有一个接受BigDecimal的构造函数.在测试中运行时,我得到了

org.hibernate.QueryException: Parameters are only supported in SELECT clauses when used as part of a INSERT INTO DML statement
    at org.hibernate.hql.internal.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:146)
Run Code Online (Sandbox Code Playgroud)

将此更改为:

createQuery()
   .from(path()).
       where(specification().getPredicate())
           .list(
ConstructorExpression.create(Foo.class, NumberTemplate.create(BigDecimal.class, "1.0")));
Run Code Online (Sandbox Code Playgroud)

产生不同的堆栈跟踪:

java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysema.query.types.ConstructorExpression.newInstance(ConstructorExpression.java:133)
    at com.mysema.query.jpa.FactoryExpressionTransformer.transformTuple(FactoryExpressionTransformer.java:50)
Run Code Online (Sandbox Code Playgroud)

我尝试将Foo类构造函数更改为接受Integer并将查询修改为使用Integer以便进行测试,它确实生成了正确的查询:

createQuery()
   .from(path()).
      where(specification().getPredicate())
         .list(ConstructorExpression.create(LevelBoundary.class, NumberTemplate.create(Integer.class, "1")));
Run Code Online (Sandbox Code Playgroud)

使用NumberTemplate选择BigDecimal文字的正确方法是什么?NumberTemplate docs指定T扩展Number和Comparable但在非Integer类型上失败.如何在querydsl中正确选择常量/文字?

java orm hibernate querydsl

7
推荐指数
1
解决办法
1775
查看次数

为什么查询dsl实体路径限制为四个级别?

我目前正在使用maven apt插件来生成EntityPath基类.

      <plugin>
            <groupId>com.mysema.maven</groupId>
            <artifactId>maven-apt-plugin</artifactId>
            <version>1.0.4</version>
            <executions>
                <execution>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <phase>generate-sources</phase>
                    <configuration>
                        <outputDirectory>target/generated-sources/java</outputDirectory>
                        <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>com.mysema.querydsl</groupId>
                    <artifactId>querydsl-apt</artifactId>
                    <version>${querydsl.version}</version>
                </dependency>
                <dependency>
                    <groupId>com.mysema.querydsl</groupId>
                    <artifactId>querydsl-jpa</artifactId>
                    <classifier>apt</classifier>
                    <version>${querydsl.version}</version>
                </dependency>
            </dependencies>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

这会生成所需的Q类,它有助于构建查询谓词.但是我注意到,每当我超过四个级别时,我总是得到一个空指针异常,即:

QFoo.foo.x.y.z
Run Code Online (Sandbox Code Playgroud)

其中Z是QZ型; 生成的EntityPath也是如此.

这是QueryDSL的限制吗?

java querydsl

6
推荐指数
1
解决办法
3703
查看次数

springboot-kafka java 8时间序列化

目前与spring-kafka 2.1.8.RELEASE一起使用spring-boot 2.0.4。我想简化交换,将对象发送到kafka模板,并使用json作为格式。但是,一些需要反序列化的消息包含java.time.LocalDateTime。所以我的设置是

配置(application.yml):

spring:
  jackson:
    serialization:
      write_dates_as_timestamps: false
  kafka:
    consumer:
      group-id: foo
      enable-auto-commit: true
      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
      properties:
        spring.json.trusted.packages: my.package
    producer:
      value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      properties:
        spring.json.trusted.packages: my.package
      retries: 3
      acks: all
Run Code Online (Sandbox Code Playgroud)

至于应该工作的杰克逊依赖关系,我的依赖关系树是:

[INFO] |  |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.6:compile
[INFO] |  |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile
[INFO] |  |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.9.6:compile
[INFO] |  |  \- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.6:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.6:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.6:compile
Run Code Online (Sandbox Code Playgroud)

但是,这会产生以下错误:

org.apache.kafka.common.errors.SerializationException: Error deserializing key/value for partition Foo-0 at offset 4. If needed, …
Run Code Online (Sandbox Code Playgroud)

spring-boot spring-kafka

6
推荐指数
2
解决办法
1912
查看次数

如何在elasticsearch管道中指定文档版本?

我目前使用一个接收节点管道,如下所示:

{
    "my-pipeline": {
        "description": "pipeline for my filebeat",
        "processors": [
            {
                "json": {
                    "field": "message",
                    "add_to_root": true,
                    "on_failure": [
                        {
                            "rename": {
                                "field": "message",
                                "target_field": "originalMessage",
                                "ignore_missing": true
                            }
                        },
                        {
                            "set": {
                                "field": "indexName",
                                "value": "pipeline-errors"
                            }
                        },
                        {
                            "set": {
                                "field": "indexType",
                                "value": "pipeline-error"
                            }
                        },
                        {
                            "rename": {
                                "field": "@timestamp",
                                "target_field": "errorTimestamp",
                                "ignore_missing": true
                            }
                        }
                    ]
                }
            },
            {
                "remove": {
                    "field": "@timestamp",
                    "ignore_failure": true
                }
            },
            {
                "remove": {
                    "field": "message",
                    "ignore_failure": true
                } …
Run Code Online (Sandbox Code Playgroud)

elasticsearch

5
推荐指数
1
解决办法
365
查看次数

Jersey 2枚举作为方法参数抛出异常

我目前正在将jersey 1.x项目迁移到2.4.1,并在使用枚举作为参数(PathParam,QueryParam等)时出错.基本上这个枚举应该是基于球衣对方法参数的第三个要求

有一个名为valueOf或fromString的静态方法接受单个String参数(例如,参见Integer.valueOf(String)和java.util.UUID.fromString(String));

由于这个项目使用xsd作为契约来生成java类,我有:

<xs:simpleType name="status">
    <xs:restriction base="xs:string">
        <xs:enumeration value="ACTIVE" />
        <xs:enumeration value="INACTIVE" />
    </xs:restriction>
</xs:simpleType>
Run Code Online (Sandbox Code Playgroud)

产生:

@XmlType(name = "status")
@XmlEnum
public enum Status {

    ACTIVE,
    INACTIVE;

    public String value() {
        return name();
    }

    public static Status fromValue(String v) {
        return valueOf(v);
    }

}
Run Code Online (Sandbox Code Playgroud)

当它用于:

@GET
public Response search(@QueryParam("status") my.package.Status status) {
//..other code here
}
Run Code Online (Sandbox Code Playgroud)

它产生:

org.glassfish.jersey.server.internal.inject.ExtractorException: Error unmarshalling JAXB object of type "class my.package.Status".
    at org.glassfish.jersey.server.internal.inject.JaxbStringReaderProvider$RootElementProvider$1.fromString(JaxbStringReaderProvider.java:195)
    at org.glassfish.jersey.server.internal.inject.AbstractParamValueExtractor.convert(AbstractParamValueExtractor.java:138)
    at org.glassfish.jersey.server.internal.inject.AbstractParamValueExtractor.fromString(AbstractParamValueExtractor.java:129)
    at org.glassfish.jersey.server.internal.inject.SingleValueExtractor.extract(SingleValueExtractor.java:83)
    at org.glassfish.jersey.server.internal.inject.QueryParamValueFactoryProvider$QueryParamValueFactory.provide(QueryParamValueFactoryProvider.java:88)
    at org.glassfish.jersey.server.spi.internal.ParameterValueHelper.getParameterValues(ParameterValueHelper.java:81)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$AbstractMethodParamInvoker.getParamValues(JavaResourceMethodDispatcherProvider.java:121) …
Run Code Online (Sandbox Code Playgroud)

java jaxb2 jersey-2.0

4
推荐指数
1
解决办法
3653
查看次数

如何使用模拟测试现有的骆驼端点?

我目前正在使用 Camel 的模拟组件,我想在现有的路线上测试它。基本上我想保留应用程序中定义的现有路由,但在测试期间注入一些模拟,以验证或至少查看当前的交换内容。

基于文档和 Apache Camel Cookbook。我尝试过使用@MockEndpoints

这是路线构建器

@Component
public class MockedRouteStub extends RouteBuilder {

    private static final Logger LOGGER = LoggerFactory.getLogger(MockedRouteStub.class);

    @Override
    public void configure() throws Exception {
        from("direct:stub")
            .choice()
                .when().simple("${body} contains 'Camel'")
                    .setHeader("verified").constant(true)
                    .to("direct:foo")
                .otherwise()
                    .to("direct:bar")
                .end();

        from("direct:foo")
            .process(e -> LOGGER.info("foo {}", e.getIn().getBody()));

        from("direct:bar")
            .process(e -> LOGGER.info("bar {}", e.getIn().getBody()));

    }

}
Run Code Online (Sandbox Code Playgroud)

这是我的测试(目前是一个 springboot 项目):

@RunWith(SpringRunner.class)
@SpringBootTest
@MockEndpoints
public class MockedRouteStubTest {

    @Autowired
    private ProducerTemplate producerTemplate;

    @EndpointInject(uri = "mock:direct:foo")
    private MockEndpoint mockCamel;

    @Test
    public void test() throws InterruptedException { …
Run Code Online (Sandbox Code Playgroud)

apache-camel spring-test spring-boot

3
推荐指数
1
解决办法
5852
查看次数

JPA Criteria API group_concat用法

我目前正在编写一个报告,其中一个字段需要group_concat.

CriteriaQuery<GameDetailsDto> criteriaQuery = criteriaBuilder
                .createQuery(GameDetailsDto.class);
Root<BetDetails> betDetails = criteriaQuery.from(BetDetails.class);
Expression<String> betSelection = betDetails.get("winningOutcome");
criteriaQuery.multiselect(
    // other fields to select
    criteriaBuilder.function("group_concat", String.class, betSelection),
    // other fields to select
    );
//predicate, where clause and other filters

TypedQuery<GameDetailsDto> typedQuery = entityManager.createQuery(criteriaQuery);
Run Code Online (Sandbox Code Playgroud)

这会在行上抛出一个空指针异常:

TypedQuery<GameDetailsDto> typedQuery = entityManager.createQuery(criteriaQuery);
Run Code Online (Sandbox Code Playgroud)

我错误地使用了criteriaBuilder的函数方法吗?
文件说:

function(String name, Class<T> type, Expression<?>... args);
Run Code Online (Sandbox Code Playgroud)

jpa group-concat

2
推荐指数
1
解决办法
6138
查看次数