小编Pav*_*hov的帖子

org.springframework.data.mapping.PreferredConstructor$Parameter@2514e415 没有名称!(Spring数据mongo转换器)

我的 Mongo 集合中有这个 json(作为主对象的字段):

"dateOfBirth" : {
        "year" : 1953,
        "month" : 4,
        "day" : 26
    }
Run Code Online (Sandbox Code Playgroud)

我有这个用于映射的 Java 类:

@Data
public class LocalDate {

    private Integer year;
    private Integer month;
    private Integer day;

    public LocalDate(Integer year, Integer month, Integer day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public LocalDate() {
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我使用这个字段,一切正常。spring data 将 JSON 映射到我的 LocalDate

    @Field("dateOfBirth")
    private com.my.LocalDate dateOfBirth;
Run Code Online (Sandbox Code Playgroud)

但我想将此字段用作 java.time.LocalDate 并且我创建了转换器:

@ReadingConverter
public class MyLocalDateToJavaLocalDateConverter implements Converter<LocalDate, java.time.LocalDate> {
    @Override
    public …
Run Code Online (Sandbox Code Playgroud)

converters mongodb spring-data-mongodb spring-boot spring-mongodb

8
推荐指数
0
解决办法
2064
查看次数

springdoc-openapi-ui + swagger 不理解 @PathVariable required = false 标志

我使用这个库来生成文档:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.5.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我有这个控制器:

@RestController
public class TestController {

    @GetMapping("/test{hz}")
    public String test(@PathVariable(value = "hz", required = false) String hz) {
        return "test";
    }
}
Run Code Online (Sandbox Code Playgroud)

但我得到这个文档:

在此输入图像描述

为什么required = false不起作用?

我试过这个:

@RestController
public class TestController {

    @GetMapping("/test{hz}")
    public String test(
            @Parameter(description = "foo", required = false)
            @PathVariable(value = "hz", required = false) String hz) {
        return "test";
    }
}
Run Code Online (Sandbox Code Playgroud)

也不起作用

编辑:(@Helen 评论的答案)-我当然知道这一点:

@RestController
public class TestController {

    @GetMapping(value = {"/test", "/test{hz}"})
    public String test(
            @Parameter(description …
Run Code Online (Sandbox Code Playgroud)

spring swagger spring-restcontroller springdoc springdoc-openui

8
推荐指数
1
解决办法
5679
查看次数

Swagger 阅读器抛出 java.lang.NoSuchMethodError

我正在尝试在我的项目(Sparkjava)中创建 API 文档。[我使用这篇文章][1]我写了这个解析器:

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.jaxrs.Reader;
import io.swagger.models.*;
import spark.Route;

import java.util.Arrays;
import java.util.List;

public class SwaggerParser {

    public static String getSwaggerJson(List<Route> routes) throws JsonProcessingException {
        Swagger swagger = getSwagger(routes);
        return swaggerToJson(swagger);
    }

    private static Swagger getSwagger(List<Route> routes) {
        Swagger swagger = new Swagger();
        swagger.info(new Info().description("User API")
                .version("V1.0")
                .title("Some random api for testing")
                .contact(new Contact().name("Serol").url("https://serol.ro")));
        swagger.schemes(Arrays.asList(Scheme.HTTP, Scheme.HTTPS));
        swagger.consumes("application/json");
        swagger.produces("application/json");
        swagger.tag(new Tag().name("swagger"));

        Reader reader = new Reader(swagger);

        for (Route route : routes) {
            try {
                reader.read(route.getClass()); …
Run Code Online (Sandbox Code Playgroud)

java jackson swagger

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

SyntaxError:从 IntellijIDEA 启动 NodeJS 时不能在模块外使用 import 语句

我尝试通过 react js 创建 hello world 应用程序。我在 IntelliJ IDEA 中创建了 NodeJS 应用程序。创建一个 helloworld.js 文件。并将此代码添加到此文件中

import React from 'react';

ReactDOM.render(
    <h1>Hello, world!</h1>,
    document.getElementById('root')
);
Run Code Online (Sandbox Code Playgroud)

向 package.json 添加了 react-dom 依赖项。制作了 npm install 命令。开始申请

{
  "name": "testjsapp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react-dom": "^16.12.0"
  }
}
Run Code Online (Sandbox Code Playgroud)

错误:

"C:\Program Files\nodejs\node.exe" D:\projects\testjsapp\hello\helloworld.js
D:\projects\testjsapp\hello\helloworld.js:2
import React from 'react';
^^^^^^

SyntaxError: Cannot use import statement outside …
Run Code Online (Sandbox Code Playgroud)

javascript intellij-idea node.js reactjs

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

重构方法并删除额外的局部变量(删除绑定)

我有这门课:

public class User {
    private String name;
    private int age;
    //getters setters
}
Run Code Online (Sandbox Code Playgroud)

我有一个方法,它更新用户对象:

public void foo(User user) { 
    boolean needUpdate = false;
    
    if(needUpdateName(user.getName())) {
        user.setName("new name"); 
        needUpdate = true;
    }
    
    if(needUpdateAge(user.getAge())) {
        user.setAge(42);
        needUpdate = true; 
    }
    
    if(needUpdate) {
        userRepository.update(user);
    } 
}
Run Code Online (Sandbox Code Playgroud)

这是一个简单的例子,仅作为示例。如何重构这段代码并删除needUpdate变量?

java methods refactoring

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

JavaDoc 中`.` 和`#` 有什么区别?

我在上面的 Javadoc 方法中有这一行:

* {@link client.navigator.URLManager.newToken(NavigationToken)}
Run Code Online (Sandbox Code Playgroud)

和 Intellij IDEA 分析器将其突出显示为错误:

无法解析符号“client.navigator.URLManager.newToken”

但如果我改变.#它是确定。

* {@link client.navigator.URLManager#newToken(NavigationToken)}
Run Code Online (Sandbox Code Playgroud)

有什么不同?因为我在项目中有很多地方有.和有#

java javadoc

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

Apache HttpClient 抛出 java.net.SocketException:如果我将其用作单例,则连接重置

我创建了一个 Apache HTTP 客户端

             CloseableHttpClient client = HttpClients.custom()
                .setMaxConnPerRoute(25)
                .setMaxConnTotal(50)
                .setDefaultRequestConfig(RequestConfig.custom()
                        .setConnectionRequestTimeout(20_000)
                        .build())
                .build();
Run Code Online (Sandbox Code Playgroud)

我将它用作单例。我有这种发送请求的方法:

public RestResponse sendPost(String serverUrl, String body) throws RestException {
        try {
            HttpPost httpPost = new HttpPost(serverUrl);
            httpPost.setEntity(new StringEntity(body));

            try (CloseableHttpResponse response = client.execute(httpPost)) {
                RestResponse restResponse = new RestResponse();
                restResponse.setCode(response.getStatusLine().getStatusCode());
                restResponse.setBody(EntityUtils.toString(response.getEntity()));
                return restResponse;
            }
        } catch (Exception e) {
            throw new RestException(e);
        }
    }
Run Code Online (Sandbox Code Playgroud)

效果很好。但是闲置一段时间(5-6 分钟)后,如果我发送请求,我会得到"java.net.SocketException: Connection reset"

我有 2 个问题

  1. 我怎样才能找到这个时间开始的地方?这些参数对我不起作用

                 .setSocketTimeout(25_000)
                 .setConnectTimeout(26_000)
    
    Run Code Online (Sandbox Code Playgroud)
  2. 解决这个问题的最佳方法是什么?(我的意思是在每个请求之后或之前重试请求或更改超时或重新连接)

java socket-timeout-exception apache-httpcomponents apache-httpclient-4.x

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

如何遵循鲍勃叔叔的规则(建议)在一种方法中正确使用一个 try-catch 块?

例如我有一个方法

\n
void process(String userId) {\n  if(userId == null) throw new IlligalArgumentException("User ID is required");\n\n  User user = userService.findUserById(userId);\n\n  if(user == null) throw new UserNotFoundException("User with ID: "+ userId +" not found");\n  \n  try {\n    DataResponse response = analyticsAPI.loadAnalytics(userId, user.getDob(), user.getFirstName());  \n\n    //logic\n  } catch(AnalyticsAPIException e) {\n     //logic\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. IlligalArgumentException未经检查的异常
  2. \n
  3. UserNotFoundException未经检查的异常
  4. \n
  5. AnalyticsAPIException已检查的异常
  6. \n
\n

我读到,最好的做法是从 try 开始该方法并以 catch 结束,而不是在一个方法中乘以 try-catch 块。

\n
\n

优先使用异常而不是错误代码 我们更喜欢使用异常而不是错误代码\n因为它们更加明确。在处理 try / …

java architecture refactoring exception clean-architecture

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