如果我们使用Spring MVC开发REST,它将支持XML和JSON数据.我在spring config bean中编写了ContentNegotiationViewResorverapp-servlet.xml
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"
p:order="1">
<property name="mediaTypes">
<map>
<entry key="xml" value="application/xml" />
<entry key="json" value="application/json" />
</map>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<property name="marshaller">
<bean class="org.springframework.oxm.xstream.XStreamMarshaller"
p:autodetectAnnotations="true" />
</property>
</bean>
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</list>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
我的Spring REST控制器是:
@Controller
@RequestMapping("/rest/customers")
class CustomerRestController {
protected Log log = LogFactory.getLog(CustomerRestController.class);
@RequestMapping(method = POST)
@ResponseStatus(CREATED)
public void createCustomer(@RequestBody Customer customer,
HttpServletResponse response) {
log.info(">>>" + customer.getName());
response.setHeader("Location", String.format("/rest/customers/%s",
customer.getNumber()));
}
@RequestMapping(value = "/{id}", method = GET)
@ResponseBody
public Customer …Run Code Online (Sandbox Code Playgroud) 我想动态添加Primefaces组件.我正在使用与此类似的解决方案,前面已经讨论过:
<h:form>
<h:panelGrid columns="2">
<p:dataGrid id="categoriesGrid" value="#{bean.categories}"
var="categoryBean" rowIndexVar="rowIndex">
<p:column>
<p:selectOneMenu id="categorySelect" effect="drop"
value="#{categoryBean.selectedCategory}" >
<f:selectItems value="#{categoryBean.availableCategories}"
var="category" itemLabel="#{category.name}"
itemValue="#{category}" />
</p:selectOneMenu>
</p:column>
</p:dataGrid>
<p:commandButton actionListener="#{bean.addNewCategory}"
value="Add category" update="categoriesGrid"/>
</h:panelGrid>
</h:form>
Run Code Online (Sandbox Code Playgroud)
但它有问题.在点击"添加类别"按钮后,我得到了回复示例:
<?xml version='1.0' encoding='UTF-8'?>
<partial-response>
<error>
<error-name>
class javax.faces.component.UpdateModelException
</error-name>
<error-message>
<![CDATA[/createTutorial.xhtml @85,65 value=
"#{categoryBean.selectedCategory}":java.util.NoSuchElementException]]>
</error-message>
</error>
</partial-response>
Run Code Online (Sandbox Code Playgroud)
提前致谢
是否有一个易于理解的教程来展示如何使用 maven 和 cxf 目标 java2ws 从 java 代码生成 WSDL?
我想mvn install在包含带@WebService注释的类的项目上执行,并在目标文件夹内的某处生成 WSDL,以便其他开发人员可以使用它来生成订阅者类。
此外,我希望将 Webservice 包含到一个 jar 中,我可以将其部署在 WebService 容器中,以便订阅者可以使用该服务。
到目前为止,我的 pom 看起来像这样:
<properties>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<cxf.version>2.2.3</cxf.version>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.version}</version>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.0.9</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>generate-wsdl</id>
<phase>process-classes</phase>
<goals>
<goal>java2ws</goal>
</goals>
<configuration>
<outputFile>${project.build.outputDirectory}/testService.wsdl</outputFile>
<className>complete.path.to.ClassName</className>
<verbose>true</verbose>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<pluginRepositories>
<pluginRepository>
<id>apache-snapshots</id>
<name>Apache SNAPSHOT Repository</name>
<url>http://repository.apache.org/snapshots/</url>
<snapshots> …Run Code Online (Sandbox Code Playgroud) 我对一个枚举类型进行了编码,当我为其运行创建的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_letter,is_octave等),但也有一些使用input.matches(regex)功能,而不是检查,看看是否输入它等于一个特定的字符串。
这是JUnit测试的开始,该测试测试处理意外常量的功能:
public class MusicTypeTest {
@Test
public void accidentalTest(){
String sharp = "^";
String flat = "_";
String …Run Code Online (Sandbox Code Playgroud) 我正在使用 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。
编辑注意:我知道这不是一个非常正确的要求,因为我们不应该知道工件内部有什么,正确的方法是声明将整个工件提取到目标目录(简洁的声明方法)。
一旦我将测试应用程序移动到高效(?)应用程序并开始在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) 我想要实现的目标是将枚举注入 Spring 管理的 bean。
我不希望我的 bean 被配置为 XML(除了这个不工作的枚举,没有理由这样做)。
我有一个简单的 Maven 项目:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.betlista</groupId>
<artifactId>test.spring.enum-injection</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>3.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.4</version>
</dependency>
<!-- testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.3.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<junit.version>4.11</junit.version>
</properties>
</project>
Run Code Online (Sandbox Code Playgroud)
我创建了简单的测试
import net.betlista.spring.enum_injection.EnumInjectionComponent;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestClass {
@Autowired
EnumInjectionComponent component; …Run Code Online (Sandbox Code Playgroud) 我正在学习Spring,现在我正在spring-jdbc命名空间中尝试Spring的<jdbc:embedded-database>标记.我总是遇到同样的错误:当我的测试数据被插入时," java.sql.SQLSyntaxErrorException:user缺少特权或对象未找到 ".
似乎未创建架构或未正确处理权限.当我使用调试器时,似乎正确执行了模式创建脚本.初始化默认数据库时,用户名和密码似乎也正确设置.
我不明白我做错了什么.
的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>spring-tests</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.2.9</version>
</dependency>
</dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)
src/test/resources/spring-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<jdbc:embedded-database id="dataSource">
<jdbc:script location="classpath:schema.sql" />
<jdbc:script location="classpath:data.sql" />
</jdbc:embedded-database>
</beans>
Run Code Online (Sandbox Code Playgroud)
src/test/resources/schema.sql
create table if not exists …Run Code Online (Sandbox Code Playgroud) 当我的应用程序启动时,我在日志中收到了这个警告
log4j:WARN Unrecognized element rollingPolicy
Run Code Online (Sandbox Code Playgroud)
试图谷歌寻找答案,但添加 apache-log4j-extras-1.1.jar 并没有帮助我。
我正在slf4j-log4j12 1.7.2与log4j 1.2.17.
我有这个 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) java ×7
spring ×3
junit ×2
maven ×2
rest ×2
cxf ×1
eclipse ×1
enums ×1
jackson ×1
java-ee ×1
jsf ×1
json ×1
log4j ×1
primefaces ×1
rhino ×1
slf4j ×1
spring-3 ×1
spring-jdbc ×1
spring-mvc ×1
syntax-error ×1
tomcat ×1
web-services ×1
wsdl ×1
xstream ×1