小编Ama*_*Dev的帖子

java 9模块从A和B读取包X.

我正在尝试使用java 9和gradle的spring boot.我无法运行我的简单代码,我得到以下提到的错误: -

Information:java: Errors occurred while compiling module 'Java9Gradle_main'
Information:javac 9-ea was used to compile java sources
Information:6/9/2017 10:40 PM - Compilation completed with 65 errors and 0 warnings in 15s 200ms
Error:java: module  reads package org.apache.commons.logging from both jcl.over.slf4j and commons.logging
Error:java: module  reads package org.apache.commons.logging.impl from both jcl.over.slf4j and commons.logging
Error:java: module spring.boot.starter.web reads package org.apache.commons.logging from both jcl.over.slf4j and commons.logging
Error:java: module spring.boot.starter.web reads package org.apache.commons.logging.impl from both jcl.over.slf4j and commons.logging
Error:java: module spring.boot.autoconfigure reads …
Run Code Online (Sandbox Code Playgroud)

spring gradle spring-boot java-9 java-module

12
推荐指数
2
解决办法
5101
查看次数

Jest酶浅意外令牌<

我正在尝试使用酶来测试反应组分,但即使用最基本的例子也无法开始.

import React from 'react'
import { shallow,render,mount,configure } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16';
import {LoginPage} from '../../../app/components/login/LoginPage'

configure({ adapter: new Adapter() });

test('should say hello',() => {
    const loginPage = shallow(<LoginPage />)
    expect(loginPage.contains('Hello').toBe(true))
})
Run Code Online (Sandbox Code Playgroud)

运行时,我收到以下错误: -

Test suite failed to run

    D:/code/github/metallica2/metallica/client/src/app/__tests__/components/login/LoginPage.test.js: Unexpected token (9
:30)
         7 |
         8 | test('should say hello',() => {
      >  9 |     const loginPage = shallow(<LoginPage />)
           |                               ^
        10 |     expect(loginPage.contains('Hello').toBe(true))
        11 | })
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?

谢谢,

阿马尔

reactjs jestjs enzyme

11
推荐指数
2
解决办法
7760
查看次数

针对特定环境的spring yml文件

我有3个yml文件

  • application-default.yml - >默认属性,应该在所有配置文件中都可用
  • application-dev.yml - >仅适用于dev配置文件的属性
  • application-prod.yml - >仅适用于prod配置文件的属性

当我通过传递启动我的启动应用程序时-Dspring.profiles.active=dev,我能够访问application-dev.yml特定的属性.但我无法获得application-default.yml文件中定义的属性.以下是我的application-dev.yml档案:

Spring:
 profiles:
  include: default

spring.profiles: dev

prop:
 key:value
Run Code Online (Sandbox Code Playgroud)

java spring yaml spring-boot

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

Spring Batch 作业未结束

我刚刚开始使用 Spring 批处理并陷入了这个问题。我的工作永无止境,无限循环。以下是代码:-

@SpringBootApplication
@EnableBatchProcessing
public class Main implements CommandLineRunner{

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private JobLauncher jobLauncher;

    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
    @Override
    public void run(String... args) throws Exception {
        jobLauncher.run(flattenPersonJob(),new JobParameters());
        System.out.println("Done");

    }

    @Bean
    public ItemReader itemReader() {
        return new PersonReader();
    }

    @Bean
    public ItemProcessor itemProcessor() {
        return new PersonProcessor();
    }

    @Bean
    public ItemWriter itemWriter() {
        return new PersonWriter();
    }

    @Bean
    public Step flattenPersonStep() {
        return stepBuilderFactory.get("flattenPersonStep").
                chunk(1).
                reader(itemReader()).
                processor(itemProcessor()). …
Run Code Online (Sandbox Code Playgroud)

java spring spring-batch spring-boot

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

为什么在java-8流减少操作中不执行组合器功能?

我试图了解流中的 reduce 方法是如何工作的。

Stream.of(1,2,3,4,5,6,7).reduce(new ArrayList<>(),
(List<Integer> l, Integer a) -> {l.add(a);return l;},
(List<Integer> l1, List<Integer> l2) -> {
System.out.println("l1 is" + l1 + "l2 is " + l2);
l1.addAll(l2);
return l1;
}).forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)

该行System.out.println("l1 is" + l1 + "l2 is " + l2)永远不会被打印。我可以理解发生了什么,(List<Integer> l, Integer a) -> {l.add(a);return l;}
有人可以解释为什么没有打印出来吗?java文档说function for combining two values, which must be compatible with the accumulator function

谢谢,阿马尔

java java-8 java-stream

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

Spring 4 mvc REST XML和JSON响应

我试图从我的控制器中的相同方法生成XML和JSON响应.我用Google搜索并发现JSON是spring的默认值,因为jackson在classpath上.但对于XML,我必须添加JAXB依赖项,然后使用JAXB注释注释我的模型.另外,我不得不更改@RequestMapping注释的produce属性.

现在,我的默认行为已更改,它返回XML响应.我想在使用值application/json将content-Type标头添加到我的请求后,我的响应将是JSON,但遗憾的是情况并非如此.以下是我的代码: -

package com.example.controller;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.domain.Person;

@RestController("person")
public class PersonController {

    @RequestMapping(name ="getperson", produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
    public Person getPerson() {
        Person p = new Person();
        p.setAge(28);
        p.setEmail("email@gmail.com");
        p.setName("Amar");
        return p;
    }

}
Run Code Online (Sandbox Code Playgroud)

型号类: -

package com.example.domain;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Person {

    //@XmlElement
    private String name;
    //@XmlElement
    private int age;
    private String email;

    public String getName() {
        return name;
    }
    @XmlElement
    public void setName(String name) {
        this.name = name;
    } …
Run Code Online (Sandbox Code Playgroud)

rest spring json spring-mvc spring-boot

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

如何在 Java 中使用 Optional?

我有一个服务,它从数据库中查询优惠券列表。此服务Optional向客户端返回。

return listOfCoupons.isEmpty() ? Optional.empty() : Optional.of(listOfCoupons.get(listOfCoupons.size() - 1));
Run Code Online (Sandbox Code Playgroud)

此代码的客户端Optional以以下方式使用命名的“优惠券”:

if (coupons.isPresent) {
   save (coupons.get());
} 
Run Code Online (Sandbox Code Playgroud)

这是正确的用法Optional吗?

java optional java-8

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

Spring启动hibernate 5集成错误

我正在尝试使用Java配置将spring Boot与Hibernate 5集成.我使用Hibernate已经有一段时间了.我得到了下面提到的例外.有人可以建议如何解决这个问题吗?

以下是我的java配置文件,后跟异常堆栈跟踪.

package com.example;

import com.example.dao.CustomerDao;
import com.example.model.Customer;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Properties;

@SpringBootApplication
@EnableTransactionManagement
public class SpringHibernateDemoApplication implements CommandLineRunner {

    public static void main(String[] args) {

        SpringApplication.run(SpringHibernateDemoApplication.class, args);
    }

    @Bean
    public DataSource dataSource() throws ClassNotFoundException {
        //SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
        // dataSource.setDriverClass((Class<? extends Driver>)Class.forName("com.mysql.jdbc.Driver"));
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/customerDB");
        dataSource.setUsername("root");
        dataSource.setPassword("welcome");
        return  dataSource;
    }

    @Bean
    public LocalSessionFactoryBean  sessionFactory() throws …
Run Code Online (Sandbox Code Playgroud)

java hibernate spring-boot

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