我花了太多时间(我会说超过10个)试图找出如何获得基本的json调用(来自angularjs)以打开并处理我的Jersey 2.4.我已经尝试了谷歌的每一个可能的结果,我仍然得到一个
415(不支持的媒体类型)
客户端和
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException:找不到媒体类型= application/json的MessageBodyReader,type = class jersey.webreadability.controllers.jsonmodels.TextInput,genericType = class jersey.webreadability.controllers.jsonmodels.TextInput.
在服务器端.
我会在这里写下我试图解决的每一个可能的文件,希望它能帮助别人帮助我.目前我并不在乎它将如何工作,只要它能够工作,我明白我应该让这个与Jackson或Gson一起工作的人.
依赖关系(来自POM文件):
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json</artifactId>
<version>2.0-m05-1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.17.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
来自Web.xml:
<servlet>
<servlet-name>webReadability</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>jersey.webreadability.controllers</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.algo.server.webservice;org.codehaus.jackson.jaxrs</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
Run Code Online (Sandbox Code Playgroud)
对象类:
@XmlRootElement(name = "TextInput")
public class TextInput implements Serializable {
@XmlElement public String url;
@XmlElement public String text;
@XmlElement public String file;
public TextInput() {
}
public TextInput(String url, String text, String file) {
this.url = url;
this.text …
Run Code Online (Sandbox Code Playgroud) 目前我们正在将应用程序升级到 Spring Boot 3,并且在运行时遇到一些记录器工厂冲突的问题。
我们使用 logback 来实现日志记录,但通过 slf4j。
我们正在拉入该spring-boot-starter-logging
模块,并且没有在 build.gradle 中指定我们自己的 slf4j 或 logback 版本。
这是一个非常复杂的项目,有很多依赖项,但日志记录依赖项都在这里:
integrationTestRuntimeClasspath - Runtime classpath of source set 'integration test'.
+--- net.logstash.logback:logstash-logback-encoder:6.6
| \--- com.fasterxml.jackson.core:jackson-databind:2.12.0 -> 2.14.2
| +--- com.fasterxml.jackson.core:jackson-annotations:2.14.2
| | \--- com.fasterxml.jackson:jackson-bom:2.14.2
| | +--- com.fasterxml.jackson.core:jackson-annotations:2.14.2 (c)
| | +--- com.fasterxml.jackson.core:jackson-core:2.14.2 (c)
| | +--- com.fasterxml.jackson.core:jackson-databind:2.14.2 (c)
| | +--- com.fasterxml.jackson.datatype:jackson-datatype-guava:2.14.2 (c)
| | +--- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.14.2 (c)
| | +--- com.fasterxml.jackson.module:jackson-module-parameter-names:2.14.2 (c)
| | +--- com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:2.14.2 (c)
| | +--- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.14.2 …
Run Code Online (Sandbox Code Playgroud) 我已经使用spock一段时间来测试我的java项目,并遇到了问题.我有一个实用程序方法来从http请求获取参数,或者如果http请求为null并且我尝试使用spock测试它,则使用空字符串.我的测试看起来像这样:
package foo.bar.test
import foo.bah.HttpRequestPropertyLoader
import spock.lang.Unroll
import javax.servlet.http.HttpServletRequest
import spock.lang.Specification
class HttpRequestPropertyLoaderTest extends Specification {
HttpRequestPropertyLoader subjectUnderTest
def result
def setup() {
subjectUnderTest = new HttpRequestPropertyLoader()
}
@Unroll("When my http request is #nullOrNot then when I get parameter from it the response=#response" )
def "Test load data from request"() {
given:
HttpServletRequest mockHttpRequest = Mock()
mockHttpRequest.getAttribute("foo") >> "bar"
when:
result = subjectUnderTest.loadStringFromHttpRequest(httpRequest, "foo")
then:
result == response
where:
httpRequest | response | nullOrNot
null | "" | "null"
mockHttpRequest …
Run Code Online (Sandbox Code Playgroud) 我有一个switch语句来处理java enum foo,并使用spock编写一些groovy单元测试.我已经添加了一个测试,它验证当前是否处理了每种类型的foo而没有抛出异常.现在我想测试一个无法识别的foo类型会导致抛出异常.
要做到这一点,我将不得不模拟一个枚举,并已经看到这里概述的解决方案:模拟 Java枚举添加一个值来测试失败的情况
我也知道可以用powermock做,但我真的很喜欢spock,因为我发现它非常轻巧,所以我们正在寻找使用spock的解决方案.
我觉得这样的事可能有用:
def "An unexpected type of foo causes an exception to be thrown"() {
given:
Foo foo = Mock()
when:
subjectUnderTest.handleFoo foo
then:
thrown Exception
}
Run Code Online (Sandbox Code Playgroud)
但是,此操作失败,并显示以下错误消息:
org.spockframework.mock.CannotCreateMockException: Cannot create mock for class com.Foo because Java mocks cannot mock final classes. If the code under test is written in Groovy, use a Groovy mock.
我想知道是否有人知道使用spock的方法,因为我找不到任何文档的解决方案.
编辑在下面的一些评论之后,我觉得最好澄清我为什么要编写这些测试.
我在一个有很多开发人员的团队中工作,很可能他们中的一个会更新枚举.我希望测试失败,如果这恰好使开发人员意识到他们需要添加逻辑来处理新的枚举类型.为此,我编写了一个测试,迭代枚举可能具有的每个可能值,将其传递给方法并验证不会抛出异常.现在,如果用户添加了新的枚举,则此测试将失败,因为没有任何东西可以处理它,因此会(希望)抛出异常.
第二个测试(我正在努力编写的那个)是为了澄清默认逻辑的工作方式与我期望的一样,并且实际上异常会被抛出.在没有创建我永远不想使用的枚举值的情况下,我能想到这样做的唯一方法是模拟枚举,并测试如果不处理枚举值则抛出异常.
代码如下所示:
Enum Foo:
public enum Foo {
ONE, TWO;
}
Run Code Online (Sandbox Code Playgroud)
处理foo的方法:
private void handleFoo(Foo foo) …
Run Code Online (Sandbox Code Playgroud) 我正在用 Kotlin 编写一个程序,将消息发布到休息端点。
val messages : List<String> = getMessageToSend();
webClient
.post()
.uri { builder -> builder.path("/message").build() }
.bodyValue(PostMessageRequest(
messages.joinToString("\n")
))
.exchange()
.block()
Run Code Online (Sandbox Code Playgroud)
但是,其余端点对发送的消息的最大大小有限制。我对 Kotlin 相当陌生,但我一直在寻找一种实用的方法来实现这一目标,但我正在努力。我知道如何用 Java 编写它,但我渴望正确地完成它。我想将messages
列表拆分为列表列表,每个列表都限制为允许的最大大小,并且仅添加整个字符串,然后单独发布它们。我已经看过类似的方法chunked
,但这似乎不够灵活,无法实现我想要做的事情。
例如,如果我的消息是[this, is, an, example]
并且限制是 10,我希望我的列表列表是[[this, is an], [example]]
任何建议将不胜感激。
我正在尝试查看 spring boot h2 控制台。但是,默认端口是 8080,并且该端口已在我的机器上使用。
有没有办法让我更改 spring boot 用于 h2 的 Web 端口?有很多关于更改路径的文档,但我找不到任何关于更改端口的信息。这是我当前的配置设置:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.h2.console.settings.web-allow-others=true
spring.jpa.database=h2
Run Code Online (Sandbox Code Playgroud)
编辑
正如下面 Atul K 所提到的,h2 Web 控制台在配置的server.port
. 但是,这对我来说不是一个选项,因为我们的 Spring Boot 应用程序对进入 Spring Boot 应用程序的所有请求执行强制性标头验证。是否可以为 h2 web 控制台和 spring boot 应用程序独立配置端口?