I'm trying to create a simple webbrowser that automatically opends a html file named "Index.html" found in the same folder the application is run.
This is the code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowser1.Navigate(#".\index.html")
End Sub
End Class
It does not work, I always get an "Expression expected" error. What am I doing wrong?
The other post did not help, I found the answer my self.
This is the answer:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowser1.Url = New Uri(String.Format("file:///{0}/index.html", CurDir))
End Sub
End Class
Related
I'm trying to create a REST service with Spring.
Everything works until I try to add a List of object (CartItem) to my main object (Cart).
This is my main object
#Entity
#Table(name="cart")
public class Cart implements Serializable{
...
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Id
#Column(name="id")
private Integer id;
/*when I add this I get the error. If I remove this, the
REST service works*/
#OneToMany(mappedBy="cart", fetch = FetchType.EAGER)
private List<CartItem> cartItems;
//getter, setter, constructors, other fields ecc.
}
This is the object inside the List:
#Entity
#Table(name="cart_item")
public class CartItem implements Serializable{
...
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Id
#Column(name="id")
private Integer id;
#OneToOne(targetEntity = Product.class, cascade = CascadeType.ALL)
#JoinColumn(referencedColumnName="productId", name="product_id" )
private Product product;
#ManyToOne
#JoinColumn(name="cart_id", nullable=false)
private Cart cart;
//getter, setter, constructors, other fields ecc.
}
This is my controller
#RestController
#RequestMapping(value="rest/cart")
public class CartRestController {
...
#RequestMapping(value = "/", method = RequestMethod.GET)
public List<Cart> readAll() {
return cartService.read();
}
...
}
I get this error:
SEVERE: Servlet.service() for servlet [dispatcher] in context with path
[/webstore] threw exception [Request processing failed; nested exception
is org.springframework.http.converter.HttpMessageNotWritableException:
Could not write JSON: Infinite recursion (StackOverflowError); nested
exception is com.fasterxml.jackson.databind.JsonMappingException:
Infinite recursion (StackOverflowError) (through reference chain:...
I suppose that I had to manage the List inside the Cart object in a particular manner, maybe because i'm using JPA, but I still didn't find a solution on the internet.
Can anyone help me?
This is a serialization recursion problem, it happens because CartItem has a bidirectional mapping back to Cart. So what happens is that
a Cart gets serialized to JSON
all the CartItems inside it get serialized to JSON
the Cart property inside CartItem get serialized to JSON
the CartItems inside the cart get serialized to json, etc. etc.
You will probably want to exclude the CartItem.cart field from serialization by marking it with the #JsonIgnore annotation.
It is only too easy to expose far too much information to the outside world if you use JPA entities directly inside your webservices. Jackson actually has a useful feature called a JsonView which allows you to define which properties get exposed, you can even tailor it per webservice call if you want.
Never ending list? Did you mean a stackOverFlow exception?
If the situation is just like I said,then you should check something like fetch type and the entities' toString() or equal() method or something like that.
For example,there are to entities named A and B and their relationship is one to many(A is the one).If you config both of their fetchType as Eager,then when jpa query A,it will query B too.But B also contains A,so jpa will query A again.This kind of circle loop will cause a stackOverFlow.
By the way, how about providing more info about your problem like the Exception name?It's too hard for me to give you a specific solution,All I can do is to tell you some experiences I have met before.
Well,I created a small project with SpringBoot 2.1.0 and MySql.
It's my cartItem
public class CartItem {
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Id
#Column(name="id")
private Integer id;
#JsonIgnore
#ManyToOne
#JoinColumn(name="cart_id", nullable=false)
private Cart cart;
}
and my cart:
public class Cart {
#GeneratedValue(strategy= GenerationType.IDENTITY)
#Id
#Column(name="id")
private Integer id;
#OneToMany(mappedBy="cart", fetch = FetchType.EAGER)
private List<CartItem> cartItems;
}
Controller is as same as you wrote.After adding a #JsonIgnore to cart filed of CartItem,circle loop is over(before i do that,the program did had a circle loop problem).
Every time you use jpa with #oneToMany,#ManyToOne or #ManyToMany,you should be careful about this problem.This circular reference case could happen when instantiating a object, printing a object or something like this.And of course there is a lot of way to solve it like changing fetch type to LAZY,adding #JsonIgnore,overriding toString() and equal() method.
I have an application in front end i use angular and back end i use Spring boot.
In my front end i must upload a CSV file that insert data in tables.
So i send data to the backend which save it.
My problem: i have a class Individus with relation #OneToMany to others class like as comptes. So when i try to get All individus with this Rest service : http://localhost:8080/api/individus, i have a parsing json data error.
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727) ~[jackson-databind-2.9.6.jar:2.9.6]
Here is my class Individus:
#Entity
public class Individu implements Serializable {
#Id
private String nui;
private int civility;
private String lastName;
private String useName;
private String firstName;
#Temporal(TemporalType.DATE)
#JsonFormat(pattern="dd/MM/yyyy")
private Date birthDate;
private String birthPlace;
private String birthCountry;
private String birthCountryLib;
private String nationality;
#OneToMany(mappedBy="individu", fetch = FetchType.LAZY)
private Collection<Compte> comptes;
#OneToMany(mappedBy="individu", fetch = FetchType.LAZY)
private Collection<Adresse> adresses;
#OneToMany(mappedBy="individu", fetch = FetchType.LAZY)
private Collection<Contact> contacts;
#OneToMany(mappedBy="individu", fetch = FetchType.LAZY)
private Collection<Iban> ibans;ode here
Is some one have a solution for me?
You didn't provide enough of the stacktrace to show what the actual error is, but I suspect that you're running into a problem with a circular graph, as the objects in your Individu class' collections (e.g. Compte, Ardresse) probably hold a reference to the parent Individu instance.
The solution in that case is to add a #JsonIgnore annotation to the children's references to the parent. This is most likely an attribute in the child that is currently marked with an #ManyToOne annotation.
this is another solution
public class Produit{.....
#ManyToOne(optional=false,fetch=FetchType.LAZY)
#JsonBackReference
private Category category;....}
public class Category{....
#OneToMany(fetch=FetchType.LAZY,mappedBy="category")
#JsonManagedReference
private Collection<Produit> produits;....}
I'm newbie in spring batch and I can't determinate what pattern for reader I would need to use. I need to create the class WSRequestClass and send it to SOAP web service.
public class WSRequestClass{
private String data1;
private String data2;
private String data3;
private String data4;
private List<ClassB> dataList;
}
To create WSRequestClass is necessary:
Read data1 and data2 from table A.
Read data3 and data4 from table B.
The List<ClassB> should be create from more complex flow. First I get data from query from table C, but the result of this query is a List<ClassA>. I need process each item of List<ClassA> and convert it to ClassB, where some attributes are calculated from ClassA. (Chunk pattern but without writer).
public class ClassA {
private Date date;
private BigDecimal amount1;
private BigDecimal amount2;
private String data;
//getters & setters
...
}
public class ClassB {
private Date date;
private BigDecimal amount1;
private BigDecimal amount2;
private BigDecimal amount3;
private BigDecimal amount4;
private String data1;
private String data2;
//getters & setters
...
}
I have found multiple examples for simples chunk pattern and tasklets, but none follows this structure. This job use java configuration and JdbcTemplate for queries. The development of the web service call it's done, my only issue is that I have to read from multiple tables and read efficiently the list, transform each item to ClassB and set to WsRequestClass.
Please guide me with the pattern to use, because common ItemReadernot work for me, and I don't know how implement the custom reader that allow me do what I want.
I think you're going about this wrong. There is a pattern in batch processing called the driving query pattern. In it, your reader reads essentially the keys for the objects. You then use processors to fill in the additional information. You can read more about this pattern in the Spring Batch documentation here: https://docs.spring.io/spring-batch/trunk/reference/html/patterns.html#drivingQueryBasedItemReaders
I have the code for speech to text written in Visual Basic, but it recognizes only first word or sentence spoken, then it stops recognizing. I want it to keep listening. How can I do that? What is the problem?
Here's the code I have for now:
Imports System.Speech
Public Class Form1
Public synth As New Speech.Synthesis.SpeechSynthesizer
Public WithEvents recognizer As New Speech.Recognition.SpeechRecognitionEngine
Dim gram As New System.Speech.Recognition.DictationGrammar()
Public Sub GotSpeech(ByVal sender As Object, ByVal phrase As System.Speech.Recognition.SpeechRecognizedEventArgs) Handles recognizer.SpeechRecognized
words.Text += phrase.Result.Text & vbNewLine
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
recognizer.LoadGrammar(gram)
recognizer.SetInputToDefaultAudioDevice()
recognizer.RecognizeAsync()
End Sub
End Class
RecognizeAsync() does a single recognition. RecognizeAsync(RecognizeMode.Multiple) will do multiple recognitions.
Imports System.Speech
Public Class Form1
Public synth As New Speech.Synthesis.SpeechSynthesizer
Public WithEvents recognizer As New Speech.Recognition.SpeechRecognitionEngine
Dim gram As New System.Speech.Recognition.DictationGrammar()
Public Sub GotSpeech(ByVal sender As Object, ByVal phrase As System.Speech.Recognition.SpeechRecognizedEventArgs) Handles recognizer.SpeechRecognized
word.Text += phrase.Result.Text + ""
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
recognizer.LoadGrammar(gram)
recognizer.SetInputToDefaultAudioDevice()
recognizer.RecognizeAsync(Recognition.RecognizeMode.Multiple)
End Sub
End Class
All Problems cleared
:)
I have a resultset class:
Public Class AResultSet
Implements IEnumerable(Of ConcreteResult)
Private _list As List(Of ConcreteResult)
Public Sub New()
_list = New List(Of ConcreteResult)
End Sub
Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of ConcreteResult) Implements System.Collections.Generic.IEnumerable(Of ConcreteResult).GetEnumerator
Return _list.GetEnumerator
End Function
Public Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
Return _list.GetEnumerator
End Function
End Class
and a linq query:
Dim res As AResultSet = (From pk In testPackages, _
pp In pk.PackagePriceCollection _
Select New ConcreteResult(pk, pp))
But I get a cast error. So if I change the
Dim res As AResultSet
To:
Dim res As IEnumerable(Of ConcreteResult)
It works. But I want to cast the linq query result to the type AResultSet, which is also an IEnumerable(Of ConrecteResult).
Or am I doing something wrong here?
The result of calling Select is not an AResultSet, which is why the cast will fail. Nothing in the query knows that you want to create a AResultSet. Just because the result and AResultSet both implement the same interface doesn't mean they're the same type.
You could create an instance of AResultSet from the results, however:
Dim query = (From pk In testPackages, _
pp In pk.PackagePriceCollection _
Select New ConcreteResult(pk, pp))
Dim res as AResultSet = new AResultSet(query.ToList)
While it's unclear to me why you might want to create a class to duplicate the functionality of List(Of T), what you are trying to do is not directly possible. You should either create an implicit (Widening) user defined cast operator in your class or create a constructor that takes an IEnumerable(Of ConcreteResult) and uses that to fill the private list field.
Public Class AResultSet
Implements IEnumerable(Of ConcreteResult)
Private list As List(Of ConcreteResult)
Private Sub New(l As List(Of ConcreteResult))
list = l
End Sub
Public Shared Widening Operator CType(seq As IEnumerable(Of ConcreteResult)) As AResultSet
Return New AResultset(seq.ToList())
End Sub
...
End Class