小编max*_*yme的帖子

java.util.Date格式SSSSSS:如果不是微秒,那么最后3位是什么?

刚刚在我的Windows(8)工作站和AIX上测试了这段代码:

    public static void main(String[] args) {
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").format(new Date()));
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").format(new Date()));
    }
Run Code Online (Sandbox Code Playgroud)

并得到类似的结果:

2013-10-07 12:53:26.000905
2013-10-07 12:53:26.000906
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下,如果不是微秒,最后的数字是什么?

注意:我与DB2数据库交互,其中使用定时列作为TIMESTAMP存储按时间顺序排列的数据,其中6位数字在秒之后,即微秒(IMO).但是所有这些"时间戳"都是通过请求以下查询来创建的:

SELECT current timestamp as currenttimestamp FROM Table ( values (1)) temp
Run Code Online (Sandbox Code Playgroud)

我想知道,鉴于上述结果,我不能只使用我的代码new Date()而不是current timestamp从数据库中选择.

谢谢.

PS:我搜索但发现没有相关(已回答)的问题,例如: java中的当前时间(以微秒为单位)以小时,分钟,秒,毫秒,微秒获取时间

java format timestamp date

31
推荐指数
2
解决办法
10万
查看次数

如何使用运行时"限定符"变量动态注入服务?

在给定运行时值的情况下,我找不到一种简单的方法来注入组件/服务.

我开始阅读@ Spring的文档:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-autowired-annotation-qualifiers 但我找不到如何变量传递给@Qualifier注释的值.

假设我有一个具有这种界面的模型实体:

public interface Case {

    String getCountryCode();
    void setCountryCode(String countryCode);

}
Run Code Online (Sandbox Code Playgroud)

在我的客户端代码中,我会做类似的事情:

@Inject
DoService does;

(...)

Case myCase = new CaseImpl(); // ...or whatever
myCase.setCountryCode("uk");

does.whateverWith(myCase);
Run Code Online (Sandbox Code Playgroud)

......我的服务是:

@Service
public class DoService {

    @Inject
    // FIXME what kind of #$@& symbol can I use here?
    // Seems like SpEL is sadly invalid here :(
    @Qualifier("${caze.countryCode}")
    private CaseService caseService;

    public void whateverWith(Case caze) {
        caseService.modify(caze);
    }

}
Run Code Online (Sandbox Code Playgroud)

我希望caseService是UKCaseService(参见下面的相关代码).

public interface CaseService {

    void modify(Case caze); …
Run Code Online (Sandbox Code Playgroud)

service spring dependency-injection autowired

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

如何将一组基元作为varargs传递?

我试图将一个基元数组(在我的例子中int[])传递给一个带有varargs的方法.

让我们说:

    // prints: 1 2
    System.out.println(String.format("%s %s", new String[] { "1", "2"}));
    // fails with java.util.MissingFormatArgumentException: Format specifier '%s'
    System.out.println(String.format("%s %s", new int[] { 1, 2 }));
Run Code Online (Sandbox Code Playgroud)

但请注意,第一行会收到以下警告:

类型String []的方法格式(String,Object ...)的最后一个参数与vararg参数类型不完全匹配.转换为Object []以确认非varargs调用,或者为varargs调用传递Object类型的各个参数.

另请注意,我不使用构造函数输入数组,但是我从封闭方法中获取它,其签名我无法更改,例如:

private String myFormat(int[] ints) {
    // whatever format it is, it's just an example, assuming the number of ints
    // is greater than the number of the format specifiers
    return String.format("%s %s %s %s", ints);
}
Run Code Online (Sandbox Code Playgroud)

java arrays variadic-functions

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

CSS选择孙子中的第一类?

我读过一些棘手的问题,比如用CSS选择第一个后代或者如何只隐藏一个类型的第一个元素?但我的不符合这些.

我有这个HTML结构:

<div class="parent">
    <div>
        <p class="interline"><!-- only this -->
        <p class="interline">
        <p class="interline">
    </div>
    <div>
        <p class="interline">
        <p class="interline">
        <p class="interline">
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

并且只想选择<p>.parent div下的第一个孙子.

我怎么做?

html css css-selectors css3

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

如何在一行代码中配置 JacksonJaxbJsonProvider?

我在 REST 应用程序中使用 Jackson 进行 JSON 序列化,如下所示:

import javax.ws.rs.core.Application;

import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
import org.codehaus.jackson.map.SerializationConfig;

public class MyApplication extends Application {

    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(MyResource1.class);
        classes.add(MyResource2.class);
        ...
        return classes;
    }

    public Set<Object> getSingletons() {
        Set<Object> singletons = new HashSet<Object>();
        singletons.add(new JacksonJaxbJsonProvider().configure(
            SerializationConfig.Feature.WRITE_NULL_PROPERTIES, false)); // (*)
        return singletons;
    }

}
Run Code Online (Sandbox Code Playgroud)

(*) 必要的否则用@XmlElementRef 注释的字段将出现在JSON 输出中,"myField": null当为空时(而不是不出现),而用@XmlElement 注释的字段也可以。

然而,SerializationConfig.Feature.WRITE_NULL_PROPERTIES不推荐使用 of以支持SerializationConfig.setSerializationInclusion(..)(1) 其本身已被弃用以支持SerializationConfig.withSerializationInclusion(..)通过 ObjectMapper (2)配置

但是我认为使用new JacksonJaxbJsonProvider().configure(..)确实配置了一个ObjectMapper(我看了一下代码)。

所以我的问题是:如何在不使用任何已弃用的属性并且不(重新)创建新的配置类的情况下正确配置它?

(1) …

java json jaxb jackson

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

如何通过 REST 控制器使用 Spring(Boot)重写 URL?

假设我有以下控制器及其父类:

@RestController
public class BusinessController extends RootController {

    @GetMapping(value = "users", produces = {"application/json"})
    @ResponseBody
    public String users() {
        return "{ \"users\": [] }"
    }

    @GetMapping(value = "companies", produces = {"application/json"})
    @ResponseBody
    public String companies() {
        return "{ \"companies\": [] }"
    }

}

@RestController
@RequestMapping(path = "api")
public class RootController {

}
Run Code Online (Sandbox Code Playgroud)

通过调用以下 URL 来检索数据:

http://app.company.com/api/users
http://app.company.com/api/companies
Run Code Online (Sandbox Code Playgroud)

现在假设我想重命名/api路径,但通过在新 URI 旁边/rest返回 HTTP 状态代码来保持其“可用”301

例如客户请求:

GET /api/users HTTP/1.1
Host: app.company.com
Run Code Online (Sandbox Code Playgroud)

服务器请求:

HTTP/1.1 301 Moved Permanently
Location: http://app.company.com/rest/users …
Run Code Online (Sandbox Code Playgroud)

spring url-rewriting http-status-code-301 spring-boot request-mapping

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

左加入Spring Data JPA规范

假设我正在上以下课:(简化为极端)

@Entity
@Table(name = "USER")
public class User {

    @OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
    private BillingAddress billingAddress;

    @OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
    private ShippingAddress shippingAddress; // This one CAN be null

}
Run Code Online (Sandbox Code Playgroud)

并且都*Address继承自此摘要:(再次简化了)

public abstract class Address {

    @OneToOne(optional = false, fetch = FetchType.LAZY)
    @JoinColumn(name = "USER_ID")
    private User user;

    @NotEmpty
    @Size(max = 32)
    @Column(name = "ADDR_TOWN")
    private String town;

 }
Run Code Online (Sandbox Code Playgroud)

我尝试了JPA规范,如Spring的博客文章所述:

/**
 * User specifications.
 * 
 * @see <a href="https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl">Advanced Spring Data JPA - Specifications …
Run Code Online (Sandbox Code Playgroud)

specifications jpa-2.0 spring-data spring-data-jpa

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

如何在 Spring Boot + GraphQL Java 工具上下文中执行对 GraphQL 的 Java 调用?

在 Spring Boot 应用程序中,我们已经拥有一个功能齐全的 GraphQL 端点,.graphqls通过 GraphQL Java 工具(我们包含依赖项)提供文件,并通过我们的基类实现和后续的graphql-spring-boot-starter处理数据解析。QueryGraphQLQueryResolverGraphQLResolver

出于业务需求,我们必须重新创建标准 REST API 端点,所以我想知道为什么不直接调用 GraphQL(而不是再次“手动”重新实现数据解析)?由于它位于同一个后端应用程序中,因此无需进行 HTTP 或 servlet (ForwardRequest) 调用,只需调用 Java 中的一些 API 即可。问题是我不知道如何继续。

我阅读了这个示例,但它使用的是基本的 GraphQL Java(不是工具): https ://www.graphql-java.com/documentation/v9/execution/

我知道这应该是可能的,因为我们可以在测试中这样做: https://github.com/graphql-java-kickstart/graphql-spring-boot/blob/master/example-graphql-tools/src/test/java /com/graphql/sample/boot/GraphQLToolsSampleApplicationTest.java

但是如何在常规代码中做到这一点呢?不存在这样的东西GraphQLTemplate

我还尝试在以下位置搜索示例:

但没有发现任何与我们的需求相关的内容。

在文档中找不到更多内容:

我错过了什么?理想情况下,我希望注入一些GraphQLSomething这样的内容:

@RestController
@RequestMapping(path = "api")
public class CompanyController {

    @Autowired
    private GraphQLSomething graphQLSomething;

    @GetMapping("company/{id}")
    public ResponseEntity<?> societe(@PathVariable @NotNull Integer id) {
        GraphQLSomethingResult result = GraphQLSomething.query("company(id: $id) { id name andsoone …
Run Code Online (Sandbox Code Playgroud)

java spring-boot graphql graphql-java graphql-java-tools

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