我有一个jersey2应用程序配置为通过杰克逊支持JSON,添加
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
在POM文件中
public MyApplication() {
...
register(JacksonFeature.class)
...
}
Run Code Online (Sandbox Code Playgroud)
在我的申请中.一切正常,我的资源将反序列化的POJO作为参数
@POST @Consumes(MediaType.APPLICATION_JSON)
public void blah(MyPojo p) {
...
}
Run Code Online (Sandbox Code Playgroud)
现在,其中一个资源需要提及杰克逊ObjectMapper自己做一些反序列化.我试过做类似的事情
@Inject
public MyResource(@Context ObjectMapper mapper) {
...
}
Run Code Online (Sandbox Code Playgroud)
要么
@GET
public String foo(@Context ObjectMapper mapper) {
...
}
Run Code Online (Sandbox Code Playgroud)
但在这两种情况下,引用都mapper为null.如何ObjectMapper在我的资源中注入引用?
我有一个第三方Web服务,我使用wsimport生成一个客户端.每次对webservice的调用都会成功完成,但我得到的响应对象的所有字段都设置为null.监控网络我可以看到,在线路上,响应消息中的所有XML元素都包含值,因此对象中应包含非空数据.此外,使用旧的axis1生成并使用相同数据调用的相同服务的客户端返回非空响应.知道发生了什么事吗?(如果它有任何区别我正在使用MOXy的JAXB实现).
更新:我已经能够缩小范围了.例如,wsdl在其自己的命名空间中定义对象http://www.acme.com/ws.我从服务中得到的回应是
<?xml version="1.0" encoding="UTF-8"?>
... SOAP envelope ...
<ns1:opINFOWLResponse xmlns:ns1="http://www.acme.com/ws"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns1:responseINFOWL xsi:type="ns1:responseINFOWL">
<result>6003</result>
<ndserr/>
<transid>61437594</transid>
<descriptionerr>BLAH.</descriptionerr>
</ns1:responseINFOWL>
</ns1:opINFOWLResponse>
... SOAP closing tags ...
Run Code Online (Sandbox Code Playgroud)
并且被解组为非null OpINFOWLResponse,它包装非null responseINFOWL对象,并且所有字段都设置为null.为了好玩,我尝试编写几行来解组上面的代码片段(在剥离SOAP开销之后)
JAXBContext ctx = JAXBContext.newInstance(OpINFOWLResponse.class);
Unmarshaller u = ctx.createUnmarshaller();
OpINFOWLResponse o = (OpINFOWLResponse) u.unmarshal(new StringReader(theSnippetAbove));
ResponseINFOWL w = o.getResponseINFOWL();
Run Code Online (Sandbox Code Playgroud)
我得到了相同的结果.如果我将上面的XML更改为
<?xml version="1.0" encoding="UTF-8"?>
<ns1:opINFOWLResponse xmlns:ns1="http://www.acme.com/ws"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns1:responseINFOWL xsi:type="ns1:responseINFOWL">
<ns1:result>6003</ns1:result>
<ns1:ndserr/>
<ns1:transid>61437594</ns1:transid>
<ns1:descriptionerr>BLAH.</ns1:descriptionerr>
</ns1:responseINFOWL>
</ns1:opINFOWLResponse>
Run Code Online (Sandbox Code Playgroud)
一切正常.游民.
更新(再次):与jaxb-RI和Moxy相同的行为.还是不知道出了什么问题.
更新(9月9日):下面关于名称空间限定错误的建议很有意思,但我认为wsimport会把事情做对.无论如何,这是我的package-info.java
@XmlSchema(
namespace = "http://www.acme.com/ws",
elementFormDefault = …Run Code Online (Sandbox Code Playgroud) 如何在球衣中使用HK2将常量注入某个类?有了Guice,我可以有类似的课程
public class DependsOnFoo {
@Inject
public DependsOnFoo(@Named("FOO") String foo) {
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
我会在注入器中配置类似的东西
bind(String.class).named("FOO").toInstance(new String("foo"))
Run Code Online (Sandbox Code Playgroud)
HK2中的等价物(如果有的话)是多少?
我有一个使用JPA持久性的jersey-2/hk2应用程序.将EntityManager在这样的约束启动
public MyApplication() {
// ...
register(new AbstractBinder() {
@Override
public void configure() {
bindFactory(EmFactory.class)
.to(EntityManager.class)
.in(RequestScoped.class);
}
});
}
Run Code Online (Sandbox Code Playgroud)
与工厂类一样
public class EmFactory implements Factory<EntityManager> {
private static final String PERSISTENCE_UNIT = "unit";
private EntityManagerFactory emf;
private CloseableService closeableService;
@Inject
public EmFactory(@Named(PERSISTENCE_UNIT) String persistenceUnit,
CloseableService closeableService) {
emf = Persistence.createEntityManagerFactory(persistenceUnit);
this.closeableService = closeableService;
}
@Override
public EntityManager provide() {
final EntityManager entityManager = emf.createEntityManager();
closeableService.add(new Closeable() {
@Override
public void close() throws IOException {
if(entityManager.isOpen()) {
entityManager.close(); …Run Code Online (Sandbox Code Playgroud) 我正在部署一个Web应用程序,它通过WebLogic容器中的jax-ws访问一些远程Web服务.在进行Web服务调用之前,我在WebLogic的控制台上看到了这些消息
Aug 30, 2011 6:48:21 PM weblogic.wsee.jaxws.framework.policy.advertisementimpl.AdvertisementHelperImplregisterExtension WARNING: Registering oracle.j2ee.ws.wsdl.extensions.addressing.AddressingExtensionRegistry extension failed; java.lang.ClassNotFoundException: oracle.j2ee.ws.wsdl.extensions.addressing.AddressingExtensionRegistry
Aug 30, 2011 6:48:21 PM weblogic.wsee.jaxws.spi.WLSServiceDelegate addWsdlDefinitionFeature SEVERE: Failed to create WsdlDefinitionFeature for wsdl location: http://192.168.192.80:19100/acme/ws/FooBarService?wsdl, error: com.sun.xml.ws.wsdl.parser.InaccessibleWSDLException, message: 2 counts of InaccessibleWSDLException.
Run Code Online (Sandbox Code Playgroud)
这让我觉得Web服务客户端代码存在问题,但是尽管存在错误,但web服务仍能正常工作.我正在用这段代码创建webservice客户端
FooBarService service = new FooBarService(
new URL("http://192.168.192.80:19100/acme/ws/FooBarService?wsdl"),
new QName("http://www.acme.com/acmews", "FooBarService"));
FooBarServiceSoap port = service.getFooBarServiceSoap();
Run Code Online (Sandbox Code Playgroud)
这些错误消息意味着什么,我应该担心它们?
跟我上一个问题的后续行动.我正在尝试使用JSR-330标准注释和与泽西捆绑的HK2框架注入应用程序配置数据.
理想情况下,我想InjectionResolver为Named注释创建一个自定义,它将在一个Map或Properties对象中查找所需的值,我将从其他地方读取的数据中填充.在我的第一次尝试中,我创建了一个Application像
public class MyApplication extends ResourceConfig {
...
packages(MY_PACKAGES);
property(MY_CONFIG_PROPERTY, someValue);
register(new AbstractBinder() {
@Override
protected void configure() {
bind(ConfigurationInjectionResolver.class)
.to(new TypeLiteral<InjectionResolver<Named>>(){})
.in(Singleton.class)
}
});
}
Run Code Online (Sandbox Code Playgroud)
然后我的InjectionResolver样子
public class ConfigurationInjectionResolver implements InjectionResolver<Named> {
@Context Application application;
@Override
public Object resolve(Injectee injectee, ServiceHandle<?> serviceHandle) {
// lookup data in application.getProperties();
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题application.getProperties()是空的.知道什么是错的吗?另外,我可以绑定我的Injector实例而不是绑定类吗?这样我就可以构造将我的Map数据作为参数传递的实例.
在我的jersey-2应用程序中,我使用的是一个非常简单的ContainerRequestFilter检查基本身份验证(可能重新发明轮子,但请耐心等待).过滤器有点像这样
@Override
public void filter(ContainerRequestContext context) throws IOException {
String authHeader = context.getHeaderString(HttpHeaders.AUTHORIZATION);
if (StringUtils.isBlank(authHeader)) {
log.info("Auth header is missing.");
context.abortWith(Response.status(Response.Status.UNAUTHORIZED)
.type(MediaType.APPLICATION_JSON)
.entity(ErrorResponse.authenticationRequired())
.build());
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想为它编写测试,模拟ContainerRequestContext对象.
@Test
public void emptyHeader() throws Exception {
when(context.getHeaderString(HttpHeaders.AUTHORIZATION)).thenReturn(null);
filter.filter(context);
Response r = Response.status(Response.Status.UNAUTHORIZED)
.type(MediaType.APPLICATION_JSON)
.entity(ErrorResponse.authenticationRequired())
.build();
verify(context).abortWith(eq(r));
}
Run Code Online (Sandbox Code Playgroud)
eq(r)即使查看Response对象的字符串表示,它们也会在调用时失败.知道什么是错的吗?
如何获取LAN上的活动IP地址,MAC地址和NetBIOS名称列表?
我想为局域网上的每个主机获取NetBIOS名称,IP和MAC地址,最好不要走到每台PC并自己记录这些东西.
如何使用Windows Script Host/PowerShell /做什么?
我有这段代码
private static final Pattern controlChars = Pattern.compile(
"[\u0001-\u0008\u000B\u000C\u000E-\u001F\u007F]");
Run Code Online (Sandbox Code Playgroud)
和IntelliJ idea(2016.3)则用红色的花样在\u0008说这是非法/不受支持的转义序列。它更改为任意的\b,\010或\x08没有影响。
我没有得到的是代码可以毫无问题地构建和运行。知道是什么问题吗?
几乎所有人都说了这一切.如何自动修复jsp页面以使相对URL映射到上下文路径而不是服务器根目录?也就是说,举例来说
<link rel="stylesheet" type="text/css" href="/css/style.css" />
Run Code Online (Sandbox Code Playgroud)
如何以一种映射css的方式设置事物my-server/my-context/css/style.css而不是my-server/css/style.css?有没有一种自动的方法,除了改变上面的所有行之外
<link rel="stylesheet" type="text/css"
href="<%= request.getContextPath() %>/css/style.css" />
Run Code Online (Sandbox Code Playgroud) java ×7
jersey-2.0 ×5
hk2 ×4
jax-ws ×2
jaxb ×1
jpa ×1
json ×1
jsp ×1
literals ×1
moxy ×1
networking ×1
powershell ×1
string ×1
sysadmin ×1
tomcat ×1
unit-testing ×1
url ×1
web-services ×1
weblogic ×1
windows ×1
wsh ×1