How to use Spring Data CouchBase 'findAll(Iterable<ID>)' Method? - couchbase

I'm trying to use Spring-Data-Couchbase.
I want to get List from findAll(Iterable) method.
I set view 'all' to my Production View.
But, I meet the exception.
How Can I use findAll(Iterable) method?
Below is a Sample Code and exception.
Document Class:
#Document
public class User {
#Id
private String id;
private String userSeq;
}
Repository Class :
public interface UserRepository extends CouchbaseRepository<User, String> {
}
Service Class:
//List<String> get the Same result & exception.
Set<String> friendSet = new HashSet<String>();
friendSet.add("User8");
friendSet.add("User6");
userRepository.findAll(friendSet)
Exception:
Caused by: java.util.concurrent.ExecutionException: OperationException: SERVER: bad_request Reason: invalid UTF-8 JSON: {{error,{2,"lexical error: invalid char in json text.\n"}},
"[User8, User6]"}
at com.couchbase.client.internal.HttpFuture.waitForAndCheckOperation(HttpFuture.java:98)
at com.couchbase.client.internal.HttpFuture.get(HttpFuture.java:82)
at com.couchbase.client.internal.HttpFuture.get(HttpFuture.java:72)
at com.couchbase.client.CouchbaseClient.query(CouchbaseClient.java:778)
... 66 more
Caused by: OperationException: SERVER: bad_request Reason: invalid UTF-8 JSON: {{error,{2,"lexical error: invalid char in json text.\n"}},
"[User8, User6]"}
at com.couchbase.client.protocol.views.NoDocsOperationImpl.parseError(NoDocsOperationImpl.java:110)
at com.couchbase.client.protocol.views.ViewOperationImpl.handleResponse(ViewOperationImpl.java:68)
at com.couchbase.client.http.HttpResponseCallback.completed(HttpResponseCallback.java:103)
at com.couchbase.client.http.HttpResponseCallback.completed(HttpResponseCallback.java:51)
at org.apache.http.concurrent.BasicFuture.completed(BasicFuture.java:115)
at org.apache.http.nio.protocol.HttpAsyncRequester$RequestExecutionCallback.completed(HttpAsyncRequester.java:376)
at org.apache.http.concurrent.BasicFuture.completed(BasicFuture.java:115)
at org.apache.http.nio.protocol.BasicAsyncClientExchangeHandler.responseCompleted(BasicAsyncClientExchangeHandler.java:179)
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.processResponse(HttpAsyncRequestExecutor.java:349)
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.inputReady(HttpAsyncRequestExecutor.java:236)
at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:267)
at org.apache.http.impl.nio.DefaultHttpClientIODispatch.onInputReady(DefaultHttpClientIODispatch.java:165)
at org.apache.http.impl.nio.DefaultHttpClientIODispatch.onInputReady(DefaultHttpClientIODispatch.java:51)
at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:113)
at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:159)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:338)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:316)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:277)
at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:105)
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:584)
... 1 more

You have to quote the id:s before you call findAll(...).
Set<String> friendSet = new HashSet<String>();
friendSet.add("\"User8\"");
friendSet.add("\"User6\"");
If the id:s aren't quoted the keys parameter sent to the Couchbase server will look something like [User8, User6] which isn't valid JSON, hence the exception. Pretty surprising behaviour...

Related

How to convert a List to JSON?

I am trying to display a List of items in JSON format. My code structure utilizing SpringBoot and JPA Repository on Server side:
Entity class
Repository class created
Service written (contains repository.findAll() function)
Controller class
Goal is to output the record set extracted from SQL database onto localhost:8080/api/getinspection.
I have added Gson dependency in my pom.xml and in my controller class added code to convert to JSON.
I get an error saying:
java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy.
Forgot to register a type adapter?
I have researched on type adapter on stackoverflow and tried to implement the solution, but in vain. Please help.
Service class
public List<INSPCTN> getInspections() {
return inspctnRepository.findAll(); }
Controller Class
#Service
public class InspectionService {
#Autowired
INSPCTNRepository inspctnRepository;
#GetMapping(path="/getInspection", produces = "application/JSON")
public String getInspections() {
List<INSPCTN> list = inspectionService.getInspections();
Gson gson = new Gson();
String json = gson.toJson(list);
return json;
}
}
Expected result: List of records from the database in JSON format
Actual:
There was an unexpected error (type=Internal Server Error, status=500).
Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy.
Forgot to register a type adapter?

How to unit test an apache camel cxfrs route?

I have a working cxfrs route. I can hit the rest service using SoapUi with a json object and get a response.
I wanted to write unit tests, and I though to use CamelTestSupport. My routebuilder configure() method looks something like this:
from(cxfrsEndpoint)
.recipientList(simple("direct:${header.operationName}"))
.setHeader(Exchange.CONTENT_LENGTH, simple("-1"));
from("direct:submitRequest")
.bean("responseBean", "checkJson")
.bean("responseBean", "createSuccessResponse");
When I hit the url from SoapUi (http://localhost:8181/cxf/myContext/submitRequest) as a POST with an appropriate json string, I get a "success" json back. Cool.
In my unit test, I created an overriden createRouteBuilder method:
#Override
protected RouteBuilder createRouteBuilder() {
MyRouteBuilder myRouteBuilder = new MyRouteBuilder();
myRouteBuilder.setCxfrsEndpoint("direct:start");
return myRouteBuilder;
}
And then my unit test (I thought) would look something like this:
#Test
public void thisIsATest() throws Exception {
MyRequest myRequest = new MyRequest();
request.setSomeProperty("Some property value");
ObjectMapper objectMapper = new ObjectMapper();
String goodJsonRequest = objectMapper.writeValueAsString(request);
String response = (String) template.requestBodyAndHeader(START_POINT, goodJsonRequest, "operationName", "submitRequest");
assertNotNull(response);
//Omitted: further assertions for content of the response json
}
Well, when I execute the unit test, I get an unmarshalling exception with this as the cause:
Caused by: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '{' (code 123) in prolog; expected '<'
at [row,col {unknown-source}]: [1,1]
at com.ctc.wstx.sr.StreamScanner.throwUnexpectedChar(StreamScanner.java:647)
at com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2054)
at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1131)
at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:164)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:415)
... 74 more
It doesn't like my json string. What is the real service receiving?
On the real route, I enabled logging on the endpoint in the blueprint.xml like this:
<!-- RouteBuilder declarations -->
<bean id="myRouteBuilder" class="com.mycompany.MyRouteBuilder">
<property name="cxfrsEndpoint" value="cxfrs:bean:cxfrsEndpoint?bindingStyle=SimpleConsumer&loggingFeatureEnabled=true" />
</bean>
The json string I see in the log as the "Payload:" doesn't look any different than the json string I'm sending via template.requestBodyAndHeader(...).
What am I missing or what am I doing wrong here?
Thanks!
Please, try not rewrite your endpoint like this:
myRouteBuilder.setCxfrsEndpoint("direct:start");
Seems to me that overriding your endpoint changes the interface and the CXF now is expecting a XML and not JSON (based on your logs):
Caused by: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '{' (code 123) in prolog; expected '<'
at [row,col {unknown-source}]: [1,1]
at com.ctc.wstx.sr.StreamScanner.throwUnexpectedChar(StreamScanner.java:647)
at com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2054)
at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1131)
at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:164)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:415)
... 74 more
You already have the endpoint defined in here:
<bean id="myRouteBuilder" class="com.mycompany.MyRouteBuilder">
<property name="cxfrsEndpoint" value="cxfrs:bean:cxfrsEndpoint?bindingStyle=SimpleConsumer&loggingFeatureEnabled=true" />
</bean>
My suggestion is to use this code as example and call your endpoint directly via HTTP:
public void setUp() throws Exception {
super.setUp();
httpclient = HttpClientBuilder.create().build();
}
public void tearDown() throws Exception {
super.tearDown();
httpclient.close();
}
#Test
public void testGetCustomerOnlyHeaders() throws Exception {
HttpGet get = new HttpGet("http://serviceurl.com");
HttpResponse response = httpclient.execute(get);
assertEquals(200, response.getStatusLine().getStatusCode());
}
If you have a chance, take a look into the entire camel-cxf project tests to have an idea how to properly test.

Spring mvc error while binding date param

I'm having a weird problem with a Spring MVC.
I have a Controller method that accepts 2 date parameters as request parameters startDate and endDate.
If I use a simple url with the 2 params like so :
http://localhost/myapp/videos?startDate=2013-05-10&endDate=2013-06-01.json
I get this error message :
[#|2013-05-27T17:39:01.711+0200|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=24;_ThreadName=Thread-2;|38386 [http-thread-pool-8080(5)] DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [public java.util.List<Video> com.ufasoli.Videos.programs(com.ufasoli.filtering.SearchParams)]: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'searchParams' on field 'endDate': rejected value [2013-06-01.json]; codes [typeMismatch.searchParams.endDate,typeMismatch.endDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [searchParams.endDate,endDate]; arguments []; default message [endDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'endDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type #org.springframework.format.annotation.DateTimeFormat java.util.Date for value '2013-06-01.json'; nested exception is java.lang.IllegalArgumentException: Invalid format: "2013-06-01.json" is malformed at ".json"]
|#]
But as soon as I remove the .json like so :
http://localhost/myapp/videos?startDate=2013-05-10&endDate=2013-06-01
it all works fine...
This looks like a bug to me as the databinder should not take the url extension into account when binding the data to the controller or is this normal behavior?
Below is the controller method causing the problem :
#RequestMapping(value = "/videos/",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public List<Videos> videos( SearchParams searchParams) {
return videosRepository.videos(searchParams);
}
Here is my SearchParams class :
public class SearchParams extends BaseSearchParams implements Serializable{
private static final long serialVersionUID = 1L;
#DateTimeFormat(iso = ISO.DATE, pattern = "yyyy-MM-dd")
private Date startDate;
#DateTimeFormat(iso = ISO.DATE, pattern = "yyyy-MM-dd")
private Date endDate;
//Setters/Getters
}
I'm using Spring MVC 3.2.1.RELEASE
Any insight?
Thanks in advance
I think that the 'url extension' (if any) should be part of the path and belongs therefore before the query: http://localhost/myapp/videos.json?startDate=2013-05-10&endDate=2013-06-01.

JAX-RS / Jersey ".get(Integer.class)" and single JSON primitive (Integer) values?

I have a JAX-RS WebService with the following method:
#Path("/myrest")
public class MyRestResource {
...
#GET
#Path("/getInteger")
#Produces(APPLICATION_JSON)
public Integer getInteger() {
return 42;
}
When accessed using this snipped:
#Test
public void testGetPrimitiveWrapers() throws IOException {
// this works:
assertEquals(new Integer(42), new ObjectMapper().readValue("42", Integer.class));
// that fails:
assertEquals(new Integer(42), resource().path("/myrest/getInteger").get(Integer.class));
}
I get the following exception:
com.sun.jersey.api.client.ClientResponse getEntity
SEVERE: A message body reader for Java class java.lang.Integer, and Java type class java.lang.Integer, and MIME media type application/json was not found
com.sun.jersey.api.client.ClientResponse getEntity
SEVERE: The registered message body readers compatible with the MIME media type are: application/json
...
The problem is just with returning single primitive values (int/boolean) or their wrapper classes. Returning other POJO classes is not the problemen so I guess all the answers regarding JSONConfiguration.FEATURE_POJO_MAPPING and JAXB annotations do not apply here.
Or which annotation should I use to describe the return type if I don't have access to its
class source?
Using ngrep I can verify that just the String "42" is returned by the webservice. Thats a valid JSON "value" but not a valid JSON "text" according to the spec. So is my problem on the client or the server side?
I tried activating JSONConfiguration natural/badgerfish according to http://tugdualgrall.blogspot.de/2011/09/jax-rs-jersey-and-single-element-arrays.html but with no success (ngrep still shows just "42"). Would that be the right path?
Any ideas are appreciated!
This is a recognized bug in Jackson, which has been touted (incorrectly in my opinion) as a feature. Why do I consider it a bug? Because while serialization works, deserialization definitely does not.
In any case, valid JSON cannot be generated from your current return type, so I would recommend creating a wrapper class:
class Result<T> {
private T data;
// constructors, getters, setters
}
#GET
#Path("/getInteger")
#Produces(APPLICATION_JSON)
public Result<Integer> getInteger() {
return new Result<Integer)(42);
}
Alternatively, you can elect to wrap root values, which will automatically encapsulate your data in a top level JSON object, keyed by the objects simple type name - but note that if this option is used that all generated JSON will be wrapped (not just for primitives):
final ObjectMapper mapper = new ObjectMapper()
.configure(SerializationFeature.WRAP_ROOT_VALUE, true)
.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
final String serializedJson = mapper.writeValueAsString(42);
final Integer deserializedVal = mapper.readValue(serializedJson,
Integer.class);
System.out.println(serializedJson);
System.out.println("Deserialized Value: " + deserializedVal);
Output:
{"Integer":42}
Deserialized Value: 42
See this answer for details on how to retrieve and configure your ObjectMapper instance in a JAX-RS environment.

Exception with REST implementation with jersey

I am building a REST service with jersey and I am stuck with a weird exception.
I want to hit a REST uri similar to:
http://localhost:9889/rest/Users/{userid}
the content to be sent with the request is in JSON similar to:
{
"attr1":"name",
"attr2":"age"
}
The endpoint url code is as shwon below:
#Path("/rest/Users")
class Users
{
#GET
#Produces(MediaType.TEXT_PLAIN)
#Path("/{userId}")
#Consumes(MediaType.APPLICATION_JSON)
public String getUserInfoQuery(
QueryDoc reqJSON,
#PathParam("userId") String userId,
#HeaderParam("Session-Token") String sessionId,
#HeaderParam("Authorization") String authToken)
)
{
.
.
.
.
}
}
And QueryDoc resource looks like this:
#XmlRootElement
#XmlAccessorType(XmlAccessType.NONE)
public class QueryDoc
{
#XmlElement(name = "attr1")
private String attr1;
#XmlElement(name = "attr2")
private String attr2;
//getters and setters
.
.
.
.
}
When I am starting the server, an exception is thrown
com.sun.jersey.api.container.ContainerException: Fatal issues found at
class com.test.Users. See logs for more details.
at com.sun.jersey.server.impl.application.WebApplicationImpl.newResourceClass(WebApplicationIm....
I could find this exception here http://www.skybert.net/java/jersey/
and as mentioned in this link..the reason is
public String getUserInfoQuery(
QueryDoc reqJSON,
reqJSON is not being annotated. If I annotate it with some annotation the exception is not thrown when server is started but in this case url response is meaningless. If i remove this parameter the url works but it doesn't consume the request JSON.
How can I make it work where I want to consume JSON content of the request as well as HeaderParams and PathParams
Is your getUserInfoQuery() method annotated with #GET annotation? If so, it is mapped to HTTP GET request. You cannot send entity in HTTP GET, so the unannotated parameter does not make sense (as Jersey maps entity to the unannotated param, but as said, in case of GET there is no entity).
Change your method getUserInfoQuery() to #PUT. In the QueryDoc class remove all annotations except #XmlRootElement. Since the attribute name you pass in the request body is same as the those in QueryDoc #XmlElement is not required. Moreover #XmlElement should be given to ge getter method.This is a good article on ReST with Jersey.
Try this:
I had the same exception with no additional details on Jersey's 'newResourceClass' method;
after hours of debugging, I realized it happened due to ambiguous URIs.
Check your URIs and eliminate any possible duplicates, such as this one:
#Path("/users")
#GET
#Produces(MediaType.APPLICATION_JSON)
public List<String> getUsers() {
...
}
#Path("/users") // BAD
#GET
#Produces(MediaType.APPLICATION_JSON)
public String getUserById(#QueryParam("userId") String userId) {
...
}