小编Bet*_*sta的帖子

语法错误:插入“枚举标识符”,插入“ EnumBody”,插入“}”

我对一个枚举类型进行了编码,当我为其运行创建的JUnit测试时,它会引发以下语法错误:

java.lang.Error: Unresolved compilation problems: 
    Syntax error, insert "enum Identifier" to complete EnumHeaderName
    Syntax error, insert "EnumBody" to complete EnumDeclaration
    Syntax error, insert "}" to complete ClassBody
Run Code Online (Sandbox Code Playgroud)

我的枚举类型具有静态函数,该函数针对特定的String返回枚举常量。这是我的一些枚举类型的代码:

public enum MusicType {

    ACCIDENTAL, LETTER, OCTAVE, REST, DUR, CHORD, TUPLET;

    public static MusicType is_accidental(String a){
        if (a=="^" | a=="_"|a=="=")
            return ACCIDENTAL;
        else return null;
    }

}
Run Code Online (Sandbox Code Playgroud)

我的静态函数的功能是非常相似的(即is_letteris_octave等),但也有一些使用input.matches(regex)功能,而不是检查,看看是否输入它等于一个特定的字符串。

这是JUnit测试的开始,该测试测试处理意外常量的功能:

public class MusicTypeTest {

    @Test
    public void accidentalTest(){
        String sharp = "^";
        String flat = "_";
        String …
Run Code Online (Sandbox Code Playgroud)

java eclipse junit enums syntax-error

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

Maven 程序集:从依赖项的特定子文件夹中复制

我正在使用 Maven 程序集插件来构建我们产品的 WAR(之前由 Ant 完成)。由于 Apache Ant 有许多遗留问题,因此有一个特定的要求可以使构建过程变得更容易:将依赖项的特定子文件夹(例如 jar 或 war 资源)复制到特定的目标子文件夹。

到目前为止,我了解到程序集描述符允许指定<outputDirectory>,但是有没有机会指定<sourceDirectory>?例如,我想将此规则应用于单个 WAR 或 JAR 类型依赖项。

考虑这个程序集描述符片段的示例(不是 100% 准确):

    <dependencySet>
        <unpack>true</unpack>
        <scope>runtime</scope>
        <useProjectArtifact>false</useProjectArtifact>
        <includes>
            <include>my-specific-dependency:war</include>
        </includes>
        <outputDirectory>WEB-INF/myresources</outputDirectory>
    </dependencySet>
Run Code Online (Sandbox Code Playgroud)

我想说我想将某些文件夹从 my-specific-dependency:war 复制到 WEB-INF/myresources。

编辑注意:我知道这不是一个非常正确的要求,因为我们不应该知道工件内部有什么,正确的方法是声明将整个工件提取到目标目录(简洁的声明方法)。

maven maven-assembly-plugin

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

带有JSON的Spring 3.1 REST:不起作用

一旦我将测试应用程序移动到高效(?)应用程序并开始在Tomcat上进行测试,我的基于Spring 3.1的REST服务就停止了工作.虽然显示了默认的index.jsp,但我的应用程序(http:// myhost:myport/test-webapp/myrestservice)无法访问,我得到了所请求的资源(/ test-webapp/myrestservice)不可用.

这是我做的:

控制器:

package com.test.webapp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.test.webap.entity.Account;

@Controller
@RequestMapping("/myrestservice")
public class AccountController{

@RequestMapping(method = RequestMethod.GET, produces="application/json")
@ResponseBody
public Account getEntity() {
            // ... <simplified>
    return new Account(//...);
}
}
Run Code Online (Sandbox Code Playgroud)

Dispatch Servlet配置:

package com.test.webapp.web;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;   
import com.test.webapp.config.AppConfig;

public class AppInit implements WebApplicationInitializer {

    private static final String DISPATCHER_SERVLET_NAME = "dispatcher";

    public void onStartup(ServletContext container) throws ServletException { …
Run Code Online (Sandbox Code Playgroud)

java rest spring json tomcat

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

parseInt 在 rhino 中返回 double

我有这个 JavaScript 文件(Rhino 1.7R4)。

importPackage(java.io);
importPackage(java.lang);
importPackage(java.util);

var reader = new BufferedReader( new InputStreamReader(System['in']) );

var line = reader.readLine();
var tok = new java.util.StringTokenizer(line);

var A = Integer.parseInt(tok.nextToken());
var B = Integer.parseInt(tok.nextToken());
var C = Integer.parseInt(tok.nextToken());

// System.out.printf( "A=%d, B=%d, C=%d\n", A, B, C );
System.out.printf( "A=%f, B=%f, C=%f\n", A, B, C );
Run Code Online (Sandbox Code Playgroud)

当我首先取消评论时printf- 我得到

A=Exception in thread "main" org.mozilla.javascript.WrappedException: Wrapped java.util.IllegalFormatConversionException: d != java.lang.Double
        at org.mozilla.javascript.Context.throwAsScriptRuntimeEx(Context.java:1754)
        at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:148)
        at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:225)
        at org.mozilla.javascript.optimizer.OptRuntime.callN(OptRuntime.java:52)
        at test._c_script_0(Unknown Source)
        at test.call(Unknown …
Run Code Online (Sandbox Code Playgroud)

rhino

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

HttpMediaTypeNotAcceptableException:找不到可接受的表示spring mvc 4.1.5 with com.fasterxml 2.5.1

帮助 - 我还能做些什么来解决这个错误?org.springframework.web.HttpMediaTypeNotAcceptableException ::无法找到可接受的表示.

我认为我的项目已正确配置以处理json restful请求.在过去的几天里,我已阅读并应用各种建议无济于事.关于我应该采取哪些不同的做法,还有其他想法吗?

我使用的是Spring MVC 4.1.5和com.fasterxml 2.5.1.

我的pom.xml的一部分

 <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.5.1</version>
    <type>bundle</type>
 </dependency>
 <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.5.1</version>
    <type>bundle</type>
 </dependency>
 <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.5.1</version>
    <type>bundle</type>
 </dependency>
Run Code Online (Sandbox Code Playgroud)

这是我的web.xml的一部分

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/appServlet/servlet-context.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

这是我的servlet-context.xml的一部分

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP …
Run Code Online (Sandbox Code Playgroud)

java spring json spring-mvc jackson

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

如何为JAR文件生成JaCoCo报告?

我们正在尝试使用JaCoCo报告针对预打包JAR文件的测试代码覆盖率。为此,我们使用java -jar附加参数启动JAR文件:

-javaagent:${project.basedir}/tools/jacocoagent.jar=output=tcpserver,port=${jacoco.port}
Run Code Online (Sandbox Code Playgroud)

JaCoCo的Maven插件配置为转储执行文件并报告结果:

-javaagent:${project.basedir}/tools/jacocoagent.jar=output=tcpserver,port=${jacoco.port}
Run Code Online (Sandbox Code Playgroud)

这样做的目的jacoco.exec是生成一个非零大小(〜350kB)的文件。

但是该报告没有显示任何内容。通过“会话”链接,我可以看到列出的JAR中的类,但是报告的主页显示了这一点: JaCoCo报告

根据日志,我们似乎正在执行代码。JaCoCo设置中缺少步骤还是应该起作用?

java code-coverage jacoco jacoco-maven-plugin

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

Spring Rest JSON绑定

我正在尝试用Spring创建Restful服务.

方法通过参数接受"UserContext"对象,即@RequestBody.

客户端使用内容类型"application/json"发送JSON对象.但我收到错误"HTTP/1.1 415不支持的媒体类型".

..甚至当客户端发送一个空的"{}"JSON对象时.

我的控制器:

@Controller
@RequestMapping(value = "/entityService")
class RestfulEntityService {

  @Resource
  private EntityService entityService;

  @ResponseBody
  @RequestMapping(value = "/getListOfEntities", method = RequestMethod.POST)
  public List<Entity> getListOfEntities(@RequestBody UserContext userContext) {
    System.out.println(userContext);
    return null;
  }
}
Run Code Online (Sandbox Code Playgroud)

UserContext.java

public class UserContext {

    private Long userId;

    private String userName;

    private UserAddress userAddress;

    private CustomerInfo customerInfo;

}
Run Code Online (Sandbox Code Playgroud)

应用背景:

  <bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"/>
  <bean id="xmlMessageConverter"
        class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
    <constructor-arg ref="xstreamMarshaller"/>
    <property name="supportedMediaTypes" value="application/xml"/>
  </bean>

  <bean id="jsonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    <property name="supportedMediaTypes" value="application/json"/>
  </bean>

  <bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
      <util:list id="beanList">
        <ref …
Run Code Online (Sandbox Code Playgroud)

java rest spring json jackson

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

context.xml中的<jdbc:embedded-database>不起作用.想要启动一个sql脚本进行单元测试

这是我的上下文文件:

<beans default-lazy-init="true" xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"> <!--Line 11-->

<tx:annotation-driven transaction-manager="transactionManager"/>



<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">    <!---->
    <property name="driverClassName" value="org.hsqldb.jdbc.JDBCDriver"/>
    <property name="url" value="jdbc:hsqldb:mem:mydb"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="hibernate.cfg.xml.incDTD"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.connection.shutdown">true</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
 </bean>

<jdbc:embedded-database id="embedded" type="HSQL"/>
<jdbc:initialize-database data-source="dataSource">
    <jdbc:script location="classpath:ctl_data-scrubd.sql"/>
</jdbc:initialize-database>
Run Code Online (Sandbox Code Playgroud)

我收到这个错误

Caused by: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 11 in XML …
Run Code Online (Sandbox Code Playgroud)

java junit spring hibernate

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

更新到 liquibase 中的特定标签

我知道我可以通过更改集在更改日志中包含版本标签

<changeSet id="1234" author="John">
    <tagDatabase tag="version_1"/>
</changeSet>
Run Code Online (Sandbox Code Playgroud)

这将允许我将数据库回滚到 version_1。

如果我有一个全新的数据库,是否可以运行更新并仅定位到一个标签?我知道我可以进行完整更新然后回滚,但这有点尴尬。

database version-control liquibase

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

是否可以删除 WS 结果中的包装元素?

我的 CXF 项目运行良好。

我的WS界面是

package net.betlista.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

import org.apache.cxf.annotations.EndpointProperty;

@WebService
public interface TestWs {

    @WebMethod
    Result foo(String child);

}
Run Code Online (Sandbox Code Playgroud)

实施是

package net.betlista.ws;

import org.springframework.stereotype.Component;

@Component("testWsEndpoint")
public class TestWsImpl implements TestWs {

    @Override
    public Result foo(final String child) {
        Result res = new Result();
        res.status = "ok";
        res.data = "bar";
        return res;
    }

}
Run Code Online (Sandbox Code Playgroud)

结果类型类:

package net.betlista.ws;

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

//@XmlTransient - NOT working
@XmlType
//@SOAPBinding(parameterStyle=SOAPBinding.ParameterStyle.BARE) - NOT working
public class Result {

    @XmlElement
    String status;

    @XmlElement …
Run Code Online (Sandbox Code Playgroud)

java web-services cxf jaxb

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