Failed to get data when accessing the rest-api method getAll [duplicate] - java

This question already has answers here:
No 'Access-Control-Allow-Origin' header is present on the requested resource- AngularJS
(12 answers)
Closed 5 years ago.
I'm developing an application which has UI from Angular2 and back-end as a rest api which is done on Spring-boot.
When I access the URL to get the list of users, it doesn't fetch all the users. When I check the browser console I get the following error:
Failed to load http://localhost:8080/api/user/getAll: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.
Any input will be appreciated.

If the frontend (Angular2) and backend (Boot) live on two different domains a solution would be to enable cors on the backend. Read here: https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/cors.html and here https://spring.io/guides/gs/rest-service-cors/

Related

Getting 400 bad Request with Tomcat 8.5.9 [duplicate]

This question already has answers here:
How to pass a JSON array as a parameter in URL
(9 answers)
Closed 5 years ago.
I'm new to tomcat server technology. Currently I'm working on the spring boot application and I tried calling the below api with the tomcat servers (below 8.5v) running in the background, I got the response as I expected. But when I tried to call the same api with tomcat server 8.5.9v running in the background I'm getting the 400 bad request.
http://localhost:8080/TestRest/ExtractTest?jsonString={"extract":
{"Type":"veswanth", "objects":[{"object":"WTT"}]}}
And in the log file I found the below issue
service Error parsing HTTP request header Note: further occurrences of
HTTP header parsing errors will be logged at DEBUG level.
java.lang.IllegalArgumentException: Invalid character found in the
request target. The valid characters are defined in RFC 7230 and RFC
3986
Kindly help me to fix this issue and correct me if did anything wrong..
You can not pass json data in url in that way. You need to pass it in body and request method should be POST.
You can refer this:
How to pass a JSON array as a parameter in URL

Using google translator in Java Application [duplicate]

This question already has answers here:
Server returned HTTP response code: 403 for URL Google Translate API
(2 answers)
Closed 5 years ago.
I tried to use the solution in the this link: How to use Google Translate API in my Java application?
The code:
Translator translate = Translator.getInstance();
String text = translate.translate("Hello!", Language.ENGLISH, Language.ROMANIAN);
System.out.println(text); // "Bună ziua!"
but the following error appears in my code:
ava.io.IOException: Server returned HTTP response code: 403 for URL: http://translate.google.com.br/translate_a/t?client=t&text=I%20am%20programmer&hl=en&sl=en&tl=pt&multires=1&prev=btn&ssel=0&tsel=0&sc=1
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at com.gtranslate.utils.WebUtils.source(WebUtils.java:24)
at com.gtranslate.parsing.ParseTextTranslate.parse(ParseTextTranslate.java:19)
at com.gtranslate.Translator.translate(Translator.java:37)
at gotranslate.GoTranslate.main(GoTranslate.java:14)
All previous libraries to use Google tools are blocked, the only way to use them is by using Google Cloud Platform:
Google returned the following error: [403] Please use Translate v2. See http://code.google.com/apis/language/translate/overview.html

when asking for a html file from browser as part of a servlet web application, browser display 404 error [duplicate]

This question already has answers here:
JSP in /WEB-INF returns "HTTP Status 404 The requested resource is not available"
(3 answers)
Closed 6 years ago.
I'm new to web application development and is trying to create a very simple example of allowing user to type in their name, submit it and return the text "hello (name)" to the user.
the text box that allow user to enter their name and the submit button are located in a html file called XmlServletForm.html.
and my servlet class look like this
however, when I type in the url address http://localhost:8084/mavenwebproject/XmlServletForm.html, the browser display the 404 error message
You should try to run at port 8080.
localhost:8080/your particular url

how to prevent a user does not turn back to a page by using back button of browser after log out [duplicate]

This question already has answers here:
Prevent user from seeing previously visited secured page after logout
(7 answers)
Closed 9 years ago.
How can i prevent browser using the cached copy of the page after log out ?
When user logged out still they can access the previous pages by using the back button
If you are using Spring Security 3.2 (or later), you can specify the following in your security namespace configuration:
<http>
<headers>
<cache-control/>
</headers>
...
</http>
This will set the necessary headers in responses stopping the browser from caching the pages in the webapp.

how to detect whether request is coming from browser or smartphone? [duplicate]

This question already has answers here:
Detecting Device Type in a web application
(8 answers)
Closed 9 years ago.
I am having a web application , which works fine .
now there is a particular page which gets bulky when i access the application from smartphone browser , so I wanted a situation like
if(request comes from computer browser client )
forward to bulky page in web application
else
if (request comes from smartphone )
forward to some other light page .
please put your suggestion how can i achieve this
Use the user-agent. Every connection to your server carries this header, by convenience and convention. There is no guarantee that a browser will be truthful(such as a spambot reporting itself as Chrome). You can get the user-agent as follows:
request.getHeader("User-Agent");
and then check again known user-agent strings and templates.
It's generally done by checking "User-Agent" header of Request

Categories