小编use*_*613的帖子

如何在使用 ResponseStatusException 时记录异常信息

我正在使用ExceptionAdviceResponseStatusException来处理我的网络应用程序中的异常情况。现在我想在抛出ResponseStatusException控制器类时记录异常信息。

我总是可以在 Controller 类中抛出异常的行附近编写日志代码:

controllerMethod(){
    logger.error("some thing happens here!");
    throw new ResponseStatusException(HttpStatus.FORBIDDEN, "some reason");
}
Run Code Online (Sandbox Code Playgroud)

但是到处编写代码太乏味了,事实上,我想要一些我在课堂上使用的模式ExceptionAdvice

@ResponseBody
@ExceptionHandler(MyException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
String myExceptionHandler(MyException e){
    logger.error("oops!", e);
    return "something";
}
Run Code Online (Sandbox Code Playgroud)

然而,ReponseStatusExceptionSpring 生成的响应具有我想要维护的格式,例如:

{
    "timestamp": "2018-02-01T04:28:32.917+0000",
    "status": 400,
    "error": "Bad Request",
    "message": "Provide correct Actor Id",
    "path": "/actor/8/BradPitt"
}
Run Code Online (Sandbox Code Playgroud)

那么,无论如何,我是否可以使用建议类进行日志记录,ResponseStatusException同时仍保持其生成的响应,或者相反,使用其他类来添加日志功能,而无需在引发异常的任何地方键入ReponseStatusExceptionlogger.error

java spring spring-mvc spring-boot

8
推荐指数
2
解决办法
2753
查看次数

如何在 spring 中注入(自动装配)bean 以使用枚举作为映射键进行映射?

我了解到在 spring 中,我可以Map<String, SomeBeanInterface>通过如下配置的名称自动连接/注入:

public interface DummyInterface{
}

@Component("impl1")
public class Impl1 implement DummyInterface{
}

@Component("impl2")
public class Impl2 implement DummyInterface{
}

public class SomeUsage{
    @Autowired
    private Map<String, DummyInterface> mapping;
    // ...
}
Run Code Online (Sandbox Code Playgroud)

并检索Componentby 字符串作为键,如:

SomeUsage use = new SomeUsage();
DummyInterface dummy = use.getMapping().get("impl1");
// do sth...
Run Code Online (Sandbox Code Playgroud)

但是,如果bean映射的关键不是的类型String,而是用户定义的类型Enum,我应该如何将bean注入到enumMap中?

我读过一些帖子,了解到它可以通过 xml 文件进行配置。但是好像是xml配置和<Enum, Bean>pair是紧耦合的,这意味着每次如果我添加一个新<Enum, Bean>对,我必须同步配置文件,与我当前的解决方案相比似乎没有区别,那就是,仍然使用<String, Bean>集合<Enum, String>并由我自己在java代码中维护映射。有没有更好的解决方案来处理这个问题?或者我错过了什么?

java spring dependency-injection autowired spring-boot

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

模拟jg指令(datalab的isGreater)

我正在做CSAPP的datalab,即isGreater功能.
这是描述

isGreater - if x > y  then return 1, else return 0
   Example: isGreater(4,5) = 0, isGreater(5,4) = 1
   Legal ops: ! ~ & ^ | + << >>
   Max ops: 24
   Rating: 3
Run Code Online (Sandbox Code Playgroud)

x和y都是int类型.
所以,我认为模拟JG指令,以实现it.Here是我的代码

int isGreater(int x, int y)
{
    int yComplement = ~y + 1;
    int minusResult = x + yComplement;  // 0xffffffff
    int SF = (minusResult >> 31) & 0x1; // 1
    int ZF = !minusResult; // 0
    int xSign = (x >> 31) …
Run Code Online (Sandbox Code Playgroud)

c assembly instructions eflags

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

如何通过 JPQL 将投影查询映射到具有 @ManyToMany @JoinTable 属性的 DTO

我有一个Blog实体,如:

@Entity
class Blog{
    @Id
    private Long id;

    private String titie;

    private String content;

    @ManyToMany
    @JoinTable(
        name = "blog_tag_association",
        joinColumns = @JoinColumn(name = "blog_id"),
        inverseJoinColumns = @JoinColumn(name = "tag_id")
    )
    private Set<Tag> tags = new LinkedHashSet<>();

    // ...
}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我使用一个单独的表来代表之间的关系BlogTag

我已经阅读了这篇文章并编写了我的自定义 JPQL 查询以将我的查询结果映射到 DTO:

class BlogSummaryDTO{
    private Long id;

    private String title;

    private Set<Tag> tags;

    public BlogSummaryDTO(Long id, String title, Set<Tag> tags){
        this.id = id;
        this.title = title;
        this.tags = tags;
    } …
Run Code Online (Sandbox Code Playgroud)

java hibernate jpa jpql spring-data-jpa

5
推荐指数
0
解决办法
386
查看次数

Debezium 的 ExtractNewRecordState 转换无法工作

我正在构建一个数据同步器,它从 MySQL Source 捕获数据更改,并将数据导出到 hive。

我选择使用 Kafka Connect 来实现这一点。我使用 Debezium 作为源连接器,使用 confluence hdfs 作为接收器连接器。

Debezium 提供了单一消息转换,让我可以after从复杂的事件消息中提取字段。我按照列出的文档进行了相同的配置,但它不起作用。

{
    // omit ...
    "transform": "unwrap",
    "transform.unwrap.type": "io.debezium.transforms.ExtractNewRecordState"
}
Run Code Online (Sandbox Code Playgroud)

我尝试在源连接器侧和接收器连接器侧配置转换,但它仍然无法工作。事实上,当我在源连接器端配置它,然后检查相应主题中的消息时,我发现消息仍然包含所有字段,包括beforesource等。

ythh@openstack2:~/confluent-5.5.0$ bin/kafka-avro-console-consumer --from-beginning --bootstrap-server localhost:9092 --topic dbserver1.test_data_1.student3
{"before":null,"after":{"dbserver1.test_data_1.student3.Value":{"id":1,"name":"ggg"}},"source":{"version":"1.1.1.Final","connector":"mysql","name":"dbserver1","ts_ms":1589005572000,"snapshot":{"string":"false"},"db":"test_data_1","table":{"string":"student3"},"server_id":1,"gtid":null,"file":"mysql-bin.000011","pos":9474,"row":0,"thread":{"long":6013},"query":null},"op":"c","ts_ms":{"long":1589005572172},"transaction":null}
{"before":null,"after":{"dbserver1.test_data_1.student3.Value":{"id":2,"name":"no way"}},"source":{"version":"1.1.1.Final","connector":"mysql","name":"dbserver1","ts_ms":1589005893000,"snapshot":{"string":"false"},"db":"test_data_1","table":{"string":"student3"},"server_id":1,"gtid":null,"file":"mysql-bin.000011","pos":11218,"row":0,"thread":{"long":6030},"query":null},"op":"c","ts_ms":{"long":1589005893773},"transaction":null}
{"before":null,"after":{"dbserver1.test_data_1.student3.Value":{"id":3,"name":"not work"}},"source":{"version":"1.1.1.Final","connector":"mysql","name":"dbserver1","ts_ms":1589005900000,"snapshot":{"string":"false"},"db":"test_data_1","table":{"string":"student3"},"server_id":1,"gtid":null,"file":"mysql-bin.000011","pos":11501,"row":0,"thread":{"long":6030},"query":null},"op":"c","ts_ms":{"long":1589005900724},"transaction":null}
Run Code Online (Sandbox Code Playgroud)

我还检查了 kafka 连接日志,这是一些输出:

ythh@openstack2:~/kafka_2.12-2.5.0/logs$ cat connect.log | grep transform
        transforms = []
        transforms = []
        transforms = []
        transforms = []
        transforms = []
        transforms = []
        transforms = []
        transforms = []
        transforms …
Run Code Online (Sandbox Code Playgroud)

apache-kafka apache-kafka-connect debezium confluent-platform

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

访问者模式适用于 shared_ptr 还是原始指针?

我正在开发一个小型编译器。现在我有一个层次系统来表达抽象语法树(AST)。

class Ast{
public:
    // ...
}

class Expr : public Ast{
public:
    // ...
}

using ExprNode = shared_ptr<Expr>;

class BinaryOp : public Expr{
public:
    ExprNode lhs;
    ExprNode rhs;
}
Run Code Online (Sandbox Code Playgroud)

Ast层次系统中的所有类都使用 shared_ptr 来管理它们的成员(如果需要),例如,BinaryOp持有两个成员来表示其操作数。

我想在树上应用访问者模式来为 AST 生成 IR 代码。我的问题是,访问者应该接受原始指针作为参数,还是接受 shared_ptr 作为参数,这可能会导入shared_from_this(考虑利弊)?如果需要 shared_ptr,我应该使用按值传递还是按引用传递?


原始指针

class AstVisitor{
public:
   virtual Ast* visit(Ast* ast);
   virtual Ast* visitBinaryOp(BinaryOp* binary){
      visit(binary->lhs.get());
      visit(binary->rhs.get());
      // ...
   }
}

class Ast{
public:
    virtual Ast* accept(AstVisitor& visitor);
}

class BinaryOp:{
public:
    virtual Ast* accept(AstVisitor& visitor) …
Run Code Online (Sandbox Code Playgroud)

c++ design-patterns

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