小编bur*_*ete的帖子

如何检查BigDecimal的位数?

要求是检查位数是否小于7位,在这种情况下插入DB,否则不.我尝试了以下解决方案:

第一解决方案

public static void checkNoOfDigitVal(BigDecimal bigDecVal) {
    BigInteger digits = bigDecVal.toBigInteger();
    BigInteger ten = BigInteger.valueOf(10);
    int count = 0;
    do {
        digits = digits.divide(ten);
        count++;
    } while (!digits.equals(BigInteger.ZERO));
    System.out.println("Number of digits : " + count);
}
Run Code Online (Sandbox Code Playgroud)

第一种解决方案有时可以正常工作,但有时候while循环中的条件不满足,并且它会继续增加计数,导致无穷无尽的计数.

二解决方案:

public static void checkNoOfDigitsVal(BigDecimal bigDecVal) {
    String string = bigDecVal.toString();
    String[] splitedString = string.split("\\.");
    String[] newVal = splitedString[0].split("");
    int a = newVal.length - 1;
    if (a <= 6) {
        System.out.println("correct size insert into DB: " + a);
    } else {
        System.out.println("Incorrect …
Run Code Online (Sandbox Code Playgroud)

java

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

如何使用实体列表来自spring-data-rest的响应

我正在使用spring-data-rest公开REST API。我的搜索方法之一返回实体列表。存储库和其余响应如下

List<Order> findByKeywordContaining(String keyword);
Run Code Online (Sandbox Code Playgroud)

搜索响应:

{  
   "_embedded":{  
      "orders":[  
         {  
            "keyword":"Iron mattress",
            "name":"Hostel",
            "_links":{  
               "self":{  
                  "href":"http://localhost:8081/orders/2"
               },
               "order":{  
                  "href":"http://localhost:8081/orders/2"
               }
            }
         },
         {  
            "keyword":"Iron",
            "name":"Weat strong",
            "_links":{  
               "self":{  
                  "href":"http://localhost:8081/orders/40"
               },
               "order":{  
                  "href":"http://localhost:8081/orders/40"
               }
            }
         }
      ]
   },
   "_links":{  
      "self":{  
         "href":"http://localhost:8081/orders/search/findByKeywordContaining?keyword=iron"
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

现在,我正在使用RestTemplate消耗此响应以在客户端进行处理,如下所示

List<Order> orders = restOperations.exchange(new URI(url), HttpMethod.GET, null, new ParameterizedTypeReference<Resource<List<Order>>>() {}).getBody().getContent();
Run Code Online (Sandbox Code Playgroud)

上面的代码对于响应中的单个对象可以正常工作,但是如果响应中包含多个对象,则上述代码可以正常工作。代码抛出以下错误

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "_embedded" (class org.springframework.hateoas.Resource), not marked as ignorable (3 known properties: , "links", "content", "page"])
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@6a2bc870; line: …
Run Code Online (Sandbox Code Playgroud)

spring jackson spring-data-rest spring-hateoas

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

如何在过滤器中更改控制器响应,以使响应结构在整个API中使用spring-boot保持一致

我使用spring-boot应用程序实现了REST API.我的所有API现在都以JSON格式为每个实体返回响应.此响应由其他服务器使用,该服务器期望所有这些响应都采用相同的JSON格式.例如;

我的所有答复都应纳入以下结构;

public class ResponseDto {
    private Object data;
    private int statusCode;
    private String error;
    private String message;
}
Run Code Online (Sandbox Code Playgroud)

目前,spring-boot以不同的格式返回错误响应.如何使用过滤器实现此目的.

错误信息格式;

{
   "timestamp" : 1426615606,
   "exception" : "org.springframework.web.bind.MissingServletRequestParameterException",
   "status" : 400,
   "error" : "Bad Request",
   "path" : "/welcome",
   "message" : "Required String parameter 'name' is not present"
}
Run Code Online (Sandbox Code Playgroud)

我需要错误和成功响应在我的spring-boot应用程序中都是相同的json结构

servlets filter spring-boot

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

SpringBoot找不到处理程序方法

我有一个基本的SpringBoot应用程序.使用Spring Initializer,嵌入式Tomcat,Thymeleaf模板引擎和包作为可执行的JAR文件.

这是主要的课程

@SpringBootApplication
public class TdkApplication {
    public static void main(String[] args) {
        SpringApplication.run(TdkApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一个控制器

@Controller
public class MockupIndexController {
    @RequestMapping("/mockup/index")
    public String welcome(Map<String, Object> model) {
        return "mockups/index";
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的 pom.xml

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
             <groupId>com.tdk.iot.core</groupId>
            <artifactId>tdk-core</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
   </dependencies>
Run Code Online (Sandbox Code Playgroud)

但是当我把它放在URL中时:

http://localhost:8080/mockup/index
Run Code Online (Sandbox Code Playgroud)

我在控制台中获得了以下日志

o.s.web.servlet.DispatcherServlet        : Servlet 'dispatcherServlet' configured successfully
o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for …
Run Code Online (Sandbox Code Playgroud)

spring-boot

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

Hibernate -validator 组序列提供程序 getDefaultSequenceProvider 获取 null 作为输入

我正在使用休眠验证器组序列,并希望根据业务规则按顺序执行组。但 groupSequenceProvider 的 getValidationGroups 输入始终为 null,因此永远不会添加自定义序列。

我的请求对象:

@GroupSequenceProvider(BeanSequenceProvider.class)
public class MyBean {

    @NotEmpty
    private String name;

    @NotNull
    private MyType type;

    @NotEmpty(groups = Special.class)
    private String lastName;

    // Getters and setters        
}
Run Code Online (Sandbox Code Playgroud)

枚举类型:

public enum MyType {
    FIRST, SECOND
}
Run Code Online (Sandbox Code Playgroud)

我的自定义序列提供者:

public class BeanSequenceProvider implements DefaultGroupSequenceProvider<MyBean> {

    @Override
    public List<Class<?>> getValidationGroups(MyBean object) {

        final List<Class<?>> classes = new ArrayList<>();
        classes.add(MyBean.class);
        if (object != null && object.getType() == MyType.SECOND) {
            classes.add(Special.class);
        }
        return classes;
    }
}
Run Code Online (Sandbox Code Playgroud)

组注释:

public interface Special {
} …
Run Code Online (Sandbox Code Playgroud)

spring hibernate-validator spring-boot

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

Hibernate @Where annotation not working with inheritance

I am using Hibernate 5.1.2

I have run into an unexpected problem that I can't seem to work around. Here's the summary of my data model: 在此处输入图片说明

dfip_project_version is my superclass table, and dfip_appln_proj_version is my subclass table. dfip_application contains a list of dfip_appln_proj_versions.

I have mapped this as follows:

@Table(name = "DFIP_PROJECT_VERSION")
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class AbstractProjectVersion {
    @Id @GeneratedValue
    @Column(name = "PROJECT_VERSION_OID")
    Long oid;

    @Column(name = "PROJ_VSN_EFF_FROM_DTM")
    Timestamp effFromDtm;

    @Column(name = "PROJ_VSN_EFF_TO_DTM")
    Timestamp effToDtm;

    @Column(name …
Run Code Online (Sandbox Code Playgroud)

java inheritance hibernate where-clause

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

百里香叶;“模板解析期间发生错误”

I\xc2\xb4m 当前在使用 springboot 时遇到问题,并且出现错误“模板解析期间发生错误(模板:“类路径资源 [templates/mainpage.html])”。

\n\n

我\xc2\xb4ve尝试重新安装不同版本的lombok,因为我认为这可能是问题所在,但到目前为止似乎没有任何效果。我\xc2\xb4m 使用 gradle 和 Eclipse 作为 IDE。\n感谢任何帮助,由于不同的 springBootVersions,发现了一些具有相同问题的线程,但尝试了旧的和新的,它也没有\xc2\xb4t 为我修复它。

\n\n

我的build.gradle看起来像这样:

\n\n
buildscript {\next {\n    springBootVersion = \'2.0.3.RELEASE\'\n}\nrepositories {\n    mavenCentral()\n}\ndependencies {\n    classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")\n}\n}\n\napply plugin: \'java\'\napply plugin: \'eclipse\'\napply plugin: \'org.springframework.boot\'\napply plugin: \'io.spring.dependency-management\'\n\ngroup = \'test\'\nversion = \'0.0.1-SNAPSHOT\'\nsourceCompatibility = 1.8\n\nrepositories {\n mavenCentral()\n}\n\ndependencies {\n  compile(\'org.springframework.boot:spring-boot-starter-thymeleaf\')\n  compile(\'org.springframework.boot:spring-boot-starter-web\')\n  runtime(\'org.springframework.boot:spring-boot-devtools\')\n  testCompile(\'org.springframework.boot:spring-boot-starter-test\')\n  compileOnly \'org.projectlombok:lombok:1.18.2\'\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的控制器

\n\n
package test;\n\nimport org.springframework.stereotype.Controller;\nimport org.springframework.ui.Model;\nimport org.springframework.web.bind.annotation.GetMapping;\nimport org.springframework.web.bind.annotation.PostMapping;\n\n@Controller\npublic class ExampleController {\n\n    @GetMapping(path = "/")\n    public String mainpage(Model model) {\n        return "mainpage";\n    }\n\n    @PostMapping(path = …
Run Code Online (Sandbox Code Playgroud)

thymeleaf spring-boot

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

无法自动接线。找不到“存储库”类型的bean

我正在使用Spring Data Jpa,这是我的项目结构:

App
  ConfigPackage
    MyConfig
  ServicePackage
    MyService
  RepositoryPackage
    MyRepository
Run Code Online (Sandbox Code Playgroud)

这是MyRepository

public interface MyRepository extends JpaRepository<MyEntity, Long> {
}
Run Code Online (Sandbox Code Playgroud)

这是MyService

@Service
public class MyService {

    @Autowired
    private MyRepository myRepository; <---here

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

这是MyConfig

@Configuration
@EnableJpaRepositories(
        basePackages = "RepositoryPackage",
        entityManagerFactoryRef = "xxx",
        transactionManagerRef = "xxx"
)
public class MyConfig {
}
Run Code Online (Sandbox Code Playgroud)

我用@Autowired注入MyRepositoryMyService,但的IntelliJ总是抱怨

无法自动接线。找不到“ MyRepository”类型的bean

即使代码可以编译并成功运行。

为什么IntelliJ无法识别这不是错误?如何清除IntelliJ的警告?

IntelliJ版本:2018.2.6

java spring intellij-idea spring-data-jpa spring-boot

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

了解 Spring Boot 执行器 `http.server.requests` 指标 MAX 属性

有人可以解释一下 MAX 统计数据在下面的回复中指的是什么吗?我没有看到它在任何地方记录。

localhost:8081/actuator/metrics/http.server.requests?tag=uri:/myControllerMethod
Run Code Online (Sandbox Code Playgroud)

回复:

{  
   "name":"http.server.requests",
   "description":null,
   "baseUnit":"milliseconds",
   "measurements":[  
      {  
         "statistic":"COUNT",
         "value":13
      },
      {  
         "statistic":"TOTAL_TIME",
         "value":57.430899
      },
      {  
         "statistic":"MAX",
         "value":0
      }
   ],
   "availableTags":[  
      {  
         "tag":"exception",
         "values":[  
            "None"
         ]
      },
      {  
         "tag":"method",
         "values":[  
            "GET"
         ]
      },
      {  
         "tag":"outcome",
         "values":[  
            "SUCCESS"
         ]
      },
      {  
         "tag":"status",
         "values":[  
            "200"
         ]
      },
      {  
         "tag":"commonTag",
         "values":[  
            "somePrefix"
         ]
      }
   ]
}
Run Code Online (Sandbox Code Playgroud)

spring-boot spring-boot-actuator spring-micrometer

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

SpringBoot:如何注入两个具有相同名称的类

在我的应用程序中,我有两个具有相同名称的类,但当然位于不同的包中。

这两个类都需要注入到应用程序中;不幸的是,我收到以下错误消息:

Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'myFeature' for bean class [org.pmesmeur.springboot.training.service.feature2.MyFeature] conflicts with existing, non-compatible bean definition of same name and class [org.pmesmeur.springboot.training.service.feature1.MyFeature]
Run Code Online (Sandbox Code Playgroud)

我的问题可以通过以下示例重现:

@Component
@EnableConfigurationProperties(ServiceProperties.class)
public class MyService implements IService {

    private final ServiceProperties serviceProperties;
    private final IProvider provider;
    private final org.pmesmeur.springboot.training.service.feature1.IMyFeature f1;
    private final org.pmesmeur.springboot.training.service.feature2.IMyFeature f2;


    @Autowired
    public MyService(ServiceProperties serviceProperties,
                     IProvider provider,
                     org.pmesmeur.springboot.training.service.feature1.IMyFeature f1,
                     org.pmesmeur.springboot.training.service.feature2.IMyFeature f2) {
        this.serviceProperties = serviceProperties;
        this.provider = provider;
        this.f1 = f1;
        this.f2 = f2;
    }
    ...
Run Code Online (Sandbox Code Playgroud)
package org.pmesmeur.springboot.training.service.feature1;

public interface IMyFeature …
Run Code Online (Sandbox Code Playgroud)

spring dependency-injection guice spring-boot

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