http://XXX:8080/SearchTermJersey/search/1/as/wat?type=product_number&type=keyword&count=4&lang=en_US
{"autoSuggestions":{"product_number":{"<span>wat</span>21000":34},"keyword":{"<span>wat</span>er":100,"<span>wat</span>er solution":50,"<span>wat</span>er purity":100}},"language":"en_US","requestDate":1393623225135,"responseDate":1393623225135,"term":"wat","version":"1"}
服务的Moxy版本的URL返回:
{"language":"en_US","requestDate":1393622174166,"responseDate":1393622174166,"term":"wat","version":"1"}
MOXy和Jackson版本的Java代码是相同的这是beanparam:
public class AutoSuggestParam {
@PathParam("version")
private String version;
@PathParam("term")
private String term;
private List<String>types;
private Integer count;
String language;
public AutoSuggestParam(@QueryParam("count")int count, @QueryParam("type")List<String>types, @QueryParam("lang")String language) {
this.types = types;
this.count = count;
this.language = language;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getTerm() {
return term;
}
public void setTerm(String term) {
this.term = term;
}
public Integer getCount() {
return count;
}
public String getLanguage() {
return language;
}
public List<String>getTypes() {
return types != null ? types : new ArrayList<String>();
}
问题似乎在于如何处理types参数。使用Jackson时,类型QueryParams被正确地封送到列表中,但MOXy失败,只返回一个NULL。因此getTypes返回一个空列表。正确处理了简单的QueryParam计数和lang。这是泽西的窃听器还是我需要和莫西做点别的什么?
@javax.ws.rs.Path("/search/{version}/as/{term}")
public class AutoSuggestResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public AutoSuggestResponse getAutoSuggest(@BeanParam AutoSuggestParam autoSuggestParam) {
System.out.printf("Request: term=%s version=%s lang=%s type=%s count=%d%n",
autoSuggestParam.getTerm(),autoSuggestParam.getVersion(), autoSuggestParam.getLanguage(), autoSuggestParam.getTypes().get(0), autoSuggestParam.getCount());
return search(autoSuggestParam);
}
private AutoSuggestResponse search(AutoSuggestParam autoSuggestParam) {
AutoSuggestResponse autoSuggestResponse = new AutoSuggestResponse();
autoSuggestResponse.setRequestDate(new Date().getTime());
autoSuggestResponse.setVersion(autoSuggestParam.getVersion());
autoSuggestResponse.setTerm(autoSuggestParam.getTerm());
autoSuggestResponse.setLanguage(autoSuggestParam.getLanguage());
int cnt = 0;
for (String type : autoSuggestParam.getTypes()) {
if ("product_number".equals(type)) {
Map<String, Object> values = autoSuggestResponse.getAutoSuggestions().get(type);
if (values == null) {
values = new LinkedHashMap<String, Object>();
autoSuggestResponse.getAutoSuggestions().put(type, values);
}
String key = String.format("<span>%s</span>21000", autoSuggestParam.getTerm());
values.put(key, 34);
cnt++;
}
else if ("keyword".equals(type)) {
Map<String, Object> values = autoSuggestResponse.getAutoSuggestions().get(type);
if (values == null) {
values = new LinkedHashMap<String, Object>();
autoSuggestResponse.getAutoSuggestions().put(type, values);
}
String key = String.format("<span>%s</span>er", autoSuggestParam.getTerm());
values.put(key, 100);
cnt++;
key = String.format("<span>%s</span>er solution", autoSuggestParam.getTerm());
values.put(key, 50);
cnt++;
key = String.format("<span>%s</span>er purity", autoSuggestParam.getTerm());
values.put(key, 100);
cnt++;
}
if (cnt >= autoSuggestParam.getCount()) {
break;
}
}
autoSuggestResponse.setResponseDate(new Date().getTime());
return autoSuggestResponse;
}
public class AutoSuggestResponse {
private Long requestDate;
private Long responseDate;
private String version;
private String term;
private String language;
private Map<String, Map<String,Object>>autoSuggestions = new LinkedHashMap<String, Map<String,Object>>();
public Long getRequestDate() {
return requestDate;
}
public void setRequestDate(Long requestDate ) {
this.requestDate = requestDate;
}
public Long getResponseDate() {
return responseDate;
}
public void setResponseDate(Long responseDate) {
this.responseDate = responseDate;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getTerm() {
return term;
}
public void setLanguage(String language) {
this.language = language;
}
public String getLanguage() {
return language;
}
public void setTerm(String term) {
this.term = term;
}
public Map<String, Map<String,Object>>getAutoSuggestions() {
return autoSuggestions;
}
}
web.xml
<display-name>MoxyAS</display-name>
<servlet>
<servlet-name>MoxyAutoSuggest</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.sial.search.ws</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<servlet-mapping>
<servlet-name>MoxyAutoSuggest</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
默认情况下,EclipseLink JAXB(MOXy)不会封送只有getter的属性。您可以添加@XmlElement
批注使其成为映射:
@XmlElement
public Map<String, Map<String,Object>>getAutoSuggestions() {
return autoSuggestions;
}
默认情况下,MOXy不使用map键作为JSON键。下面是一个示例的链接,该示例解释了如何设置此功能:
我不是在问这里已经问过的问题:@PathParam和@QueryParam之间有什么区别 这是一个“最佳做法”或惯例问题。 何时使用和。 我能想到的是,这个决定可能是利用这两者来区分信息模式。让我在下面举例说明我的LTPO-不够完美的观察。 PathParam的使用可以保留给information category,它很好地属于information Tree的一个分支。PathParam可用于向
有什么方法可以说服MOXy使用私有的XMLAdapter,还是我只能使用公有的?当然,我已经通读了文档,并尝试在谷歌上寻找解决方案,但没有任何东西跳出来。
我正在尝试为接受JSON输入的前端库创建一个java SDK。本质上,此SDK将特定类型的对象转换为JSON,然后由前端库使用。 我正在使用杰克逊的多态序列化/反序列化使用它的注释系统。 我有一个基类a和扩展a的两个子类B和C。类a有一个类型字段,我用它来决定要使用哪个类(B或C)。语法如下所示: 现在,当我使用Jackson的ObjectMapper函数读取字符串化的JSON并转换为类A时,我根
我有一个烧瓶服务器运行在http://127.0.0.1:5000和一个vuejs前端运行http://localhost:8080我已经做了api,并用postman测试了它,一切都如预期的那样工作:( > 将请求发布到/登录- (将请求发送至/登录)- 烧瓶API代码: 登录。vue: 指数vue 当我使用邮递员登录时,我得到的响应为;当我使用邮递员获取url/索引时,我得到响应。数据但当我使
我试图将Hibernate与我的spring应用程序上下文联系起来。我正在成功地连接SessionFactory,但当我尝试调用getCurrentSession时,我看到以下错误: 我正在配置数据源会话工厂和事务管理器。我还指定了事务处理将是注释驱动的 下面是我的上下文XML: null 下面是我发现错误的类: 当这被称为sessionFactory时不是null,而是带有上述错误的错误。如果我
Azure Synapse在此处发布运行时可用的JAR。我目前正在使用Apache Spark 3.1运行时。 我的项目还依赖于azure eventgrid的1.4.0版本作为依赖项(它引入了azure core)。当作业部署到Synapse上时,我遇到以下错误。 该作业在本地运行良好,但在Synapse上部署时则不行。 21/11/29 17:38:00 INFO Application Ma