我不知道为什么我不能在这里使用MultivaluedMap,有人可以提供帮助.Eclipse告诉我它无法解析为一个类型
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import javax.net.ssl.SSLContext;
import com.sun.jersey.api.client.*;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.Filterable;
import com.sun.jersey.core.util.MultivaluedMapImpl;
public class Main {
public static void main(String[] args) throws Exception {
Client client = Client.create();
WebResource webResource = client.resource("http://api.foursquare.com/v1/venues");
MultivaluedMap queryParams = new MultivaluedMapImpl();
queryParams.add("geolat", "51.543724");
queryParams.add("geolong", "-.102365");
String s = webResource.queryParams(queryParams).get(String.class);
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里缺少什么其他的东西
我正在使用Heroku服务器上的JAX-RS开发应用程序,但遇到了从服务器调用Google Books API的问题。我一直在使用Google提供的Java库进行这些调用,在某些情况下它可以工作,但在大多数情况下会返回此错误
403 Forbidden {“ code”:403,“ errors”:[{“ domain”:“ global”,“ message”:“无法确定受地理位置限制的操作的用户位置。”,“ reason”:“ unknownLocation”}],“消息”:“无法确定受地理位置限制的操作的用户位置。” }
我怀疑这个问题与Heroku用于路由的动态生成的IP地址系统有关,因为Google服务器在检测API调用的来源方面存在问题。我将如何解决这个问题?Heroku是否有使用静态IP地址的任何方法,或者我可以使用代理作为API的唯一访问点?
我习惯于使用生成wsdl文件的jax-ws,然后可以使用maven插件基于此wsdl文件及其xsd生成客户端.使用这个客户端并不麻烦,你不必真正考虑后台发生的事情,如编组,http传输等.
我目前正在使用jaxb来解组一个jax-rs项目来解组对象.其中一种方法返回一个字符串列表,但似乎jaxb不知道如何编组这个,这有点令人惊讶,因为它知道如何编组实体列表(例如,客户).
此外,我自己编写了一个jax-rs服务的客户端,使用jaxb处理http响应和有效负载的解组.使用jaxb进行编组和解组是一件非常麻烦的事情,因为它无法自动编组或解组添加到其上下文中的实体列表,甚至更少的字符串列表.
我想知道是否有一些简洁的方法可以使用restful webservices免费获得所有这些内容?这必须非常轻量级,并且客户端必须易于分发.
谢谢!Runar
使用jaxrs和jaxb无法使用的服务方法:
@GET
@Path("/{customerId}")
@Produces(MediaType.APPLICATION_XML)
public List<String> isCustomerLocked(@PathParam("customerId") Long customerId) {
}
Run Code Online (Sandbox Code Playgroud)
尝试编组/解组文本有效负载的客户端代码.添加到jaxbcontext的类未显示:
javax.xml.bind.Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(obj, stringwriter)
javax.xml.bind.Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.unmarshal(inputstream)
Run Code Online (Sandbox Code Playgroud) 我在我的RESTEasy服务中有一个方法,我想将它用作两个@GET/ @POST,它的数据可能来自查询字符串和请求体.
@GET
@POST
public String myMethod(@QueryParam("param1") @FormParam("param1") String param1,
@QueryParam("param2") @FormParam("param1") String param2) {
// ...do things
}
Run Code Online (Sandbox Code Playgroud)
但是,如果不执行以下操作,我还没有找到一种方法:
@GET
public String myMethod(@QueryParam("param1") String param1, @QueryParam("param2") String param2) {
// ...do things
}
@POST
public String myMethod2(@FormParam("param1") String param1, @FormParam("param2") String param2) {
return this.myMethod(param1, param2);
}
Run Code Online (Sandbox Code Playgroud)
有谁知道如何使第一个例子工作,或者用尽可能少的代码的另一种方法?
我在尝试用JAX-RS解决此问题时遇到了麻烦,我认为这与编组/解组过程有关(我想我不太了解),我想重新创建此:
进行发布的REST端点是/ rest / register,因此我的服务定义如下:
@ApplicationPath("/rest")
public interface RestRegistration {
@POST
@Path("/register")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
String register(UsernameRegistration usernameAccess, String network) throws RegistrationException;
@POST
@Path("/register")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
String register(EmailRegistration emailAccess, String network) throws RegistrationException;
}
public class RestRegistrationImpl implements RestRegistration {
public String register(UsernameRegistration usernameAccess, String network) throws RegistrationException {
return "StackOverflow example: username=" + usernameAccess.getUser + ", network="+network;
}
public String register(EmailRegistration emailAccess, String network) throws RegistrationException{
return "StackOverflow example: email=" + emailAccess.getEmail + ", network="+network;
}
}
Run Code Online (Sandbox Code Playgroud)它应该至少接收两个不同的JSON消息:
{ "usernameAccess" …Run Code Online (Sandbox Code Playgroud)每次我在eclipse中的java项目中更改代码行时都会收到错误:
Failed to build or refresh the JAX-RS metamodel while processing a change in the Java Model
java.lang.NullPointerException
Run Code Online (Sandbox Code Playgroud)
我不在我的java项目中使用JAX-RS!我在我的偏好中禁用了JAX-RS验证器,但错误仍然存在!
我的eclipse版本是:
面向Web开发人员的Eclipse Java EE IDE.
版本:Luna Service Release 1(4.4.1)
构建ID:20140925-1800
有什么想法吗?
我正在尝试制作一个没有web.xml的Servlet 3.0 REST服务器,即仅依赖于java类和注释.我不确定如何同时使用Spring和servlet映射注册Resource类.
我目前有:
public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
{
@Override
protected Class<?>[] getRootConfigClasses()
{
return new Class<?>[]{RootConfiguration.class};
}
@Override
protected Class<?>[] getServletConfigClasses()
{
return new Class<?>[]{RestServletConfiguration.class};
}
@Override
protected String[] getServletMappings()
{
return new String[] {"/*"};
}
}
@Configuration
@ComponentScan({"com.my.spring.beans",
"com.my.spring.services"})
public class RootConfiguration
{
}
@Configuration
@ComponentScan("com.my.resource.classes")
public class RestServletConfiguration
{
}
Run Code Online (Sandbox Code Playgroud)
当我启动应用程序时,spring bean正确连接,但我不知道如何正确设置JAX-RS servlet过滤器.我目前正在使用Jersey,并且在网上没有找到很多关于将Jersey与Spring集成而不使用web.xml的内容.
有谁知道如何以这种方式将Jersey与Spring集成?
我开发了Restful Web服务.我希望输出为XML类型(VXML).我试着这样.我使用Java 8,Maven,Jersey和新技术一样.我从该网址链接获取详细信息.
这是我的请求catch的示例代码
@Path("/custemerservice")
public interface CustemerService {
@GET
@Path("no/123")
@Consumes({ MediaType.APPLICATION_FORM_URLENCODED })
@Produces("text/html")
public Response getWelcomeInfo();
}
Run Code Online (Sandbox Code Playgroud)
以下是开发代码
@Service
public class CustemerServiceImpl implements CustemerService {
@Override
public Response getWelcomeInfo() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("user", "usul");
List<String> l = new ArrayList<String>();
l.add("light saber");
l.add("fremen clothes");
map.put("items", l);
return Response.ok(new Viewable("/cart.jsp", map)).build();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>MyApp</display-name>
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<listener>
<listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.sun.jersey.spi.container.servlet.ServletContainer</listener-class>
</listener> …Run Code Online (Sandbox Code Playgroud) 让我先提供一些背景信息.我正在开发一个与Microsoft SharePoint 2010集成的系统,不是真正的SharePoint作为系统,而是它的文件系统,文档库等的虚拟表示...用户将文件上载到SharePoint,我的系统监视这些文件并对它们编制索引进入搜索引擎(包括文件内容).用户可以通过REST接口与该系统进行交互.
我创建了一个REST接口来为用户提取一个文件,该文件对应于我的搜索引擎中的某个条目.这使用绝对网络路径作为其标识符.一个例子是//corporateserver//library1/filex.docx.由于原始策略相同,但我无法加载此文件客户端.因此,我试图通过服务器传输它.
我使用JAX-RS传输数据取得了一些成功,但是,我遇到了以下困难:
用户希望下载的文件可以是多种内容类型,其中大多数是微软办公室格式.我查看了已注册的MIME类型列表,并且遇到了application/msword或者application/vnd.ms-powerpoint
我的问题:是否有包含Microsoft Office文件的内容类型?如果没有,那么如何继续将正确的内容类型与正在请求的文件进行匹配.如果要为内容类型的word文件服务,会发生什么text/plain?
任何有关该主题的帮助将不胜感激.
编辑
我用来传输数据的代码:
package com.fujitsu.consensus.rest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import org.apache.commons.io.IOUtils;
import org.codehaus.jettison.json.JSONException;
@Path("/fetcher")
public class FetcherService {
@GET
@Path("/fetch")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response fetchFile(@QueryParam("path") String path)
throws JSONException, IOException {
final File file = new File(path);
System.out.println(path);
StreamingOutput stream = new StreamingOutput() {
@Override
public void write(OutputStream …Run Code Online (Sandbox Code Playgroud) 我已经在网站上查看了与该错误有关的其他问题,但其中大多数都是关于SessionScope的,或者没有答案。唯一可能有用的是从线程中调用bean时,作用域类型javax.enterprise.context.RequestScoped没有活动上下文,但它不在我所拥有的上下文中。
我在Wildfly 10.1(Java ee 7)上运行JAX-RS端点。看起来像这样:
@Path("")
public class ServerResource {
@Inject
Order order;
@Resource
ManagedExecutorService mes;
@PUT
@Path("/prepareOrder")
public void prepareOrder(@Suspended AsyncResponse response) {
mes.execute(() -> {
try {
Item item = new Item(); // JPA entity
order.setItem(item); // line 71
// call a service to save the order data (like item) to the DB
} catch (Exception e) {
e.printStackTrace();
response.resume(false);
}
response.resume(true);
});
}
}
Run Code Online (Sandbox Code Playgroud)
我添加try-catch仅仅是因为这个问题,它通常不存在。 Order是
@Stateful
@RequestScoped
public class Order {
private Item …Run Code Online (Sandbox Code Playgroud) jax-rs ×10
java ×6
rest ×3
marshalling ×2
cdi ×1
eclipse ×1
google-books ×1
heroku ×1
jar ×1
jaxb ×1
jersey ×1
json ×1
maven ×1
mime-types ×1
resteasy ×1
servlet-3.0 ×1
servlets ×1
spring ×1
tomcat ×1
web-services ×1
wildfly ×1