I have written a Jersey2 Java API and it is reachable.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
@Path("/test")
public class Connect {
//Vars
private Connection conn;
@GET
@Path("/test")
@Produces(MediaType.TEXT_PLAIN)
public String hello(){
return "helloWorld";
}
When i hit this in my Angular2 app using below code...
constructor(HttpWebServiceService1: HttpWebServiceService1){
HttpWebServiceService1.getHTTP()
.subscribe(
resBody => this.title = resBody.title,
error => console.error('Error: ' + error),
() => console.log('Completed!')
);
With a class for HTTP....
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class HttpWebServiceService1 {
constructor(private http: Http) {}
getHTTP() {
return this.http.get('http://localhost:8080/MyApi/test/test').map(
response => response.json());
}
}
I get an error in the browser console...
XMLHttpRequest cannot load http://localhost:8080/MyApi/test/test. No 'Access-Control-Allow-Origin' header is present on the requested resource.
Help!
Update: I imported cors-filter-2.5-SNAPSHOT.jar. I tried entering the following in my web.xml but got an error...i'm close. I know there is a way to do it in java dynamically...
<!-- CORS Filter Begin -->
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
<init-param>
<param-name>cors.allowGenericHttpRequests</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.allowOrigin</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowSubdomains</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.supportedMethods</param-name>
<param-value>GET, HEAD, POST, PUT, DELETE, OPTIONS</param-value>
</init-param>
<init-param>
<param-name>cors.supportedHeaders</param-name>
<param-value>origin, authorization, x-file-size, x-file-name, content-type, accept, x-file-type</param-value>
</init-param>
<init-param>
<param-name>cors.supportsCredentials</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.maxAge</param-name>
<param-value>3600</param-value>
</init-param>
</filter>
<!-- CORS Filter End -->
<!-- CORS Filter Mappings Begin -->
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- CORS Filter Mappings End -->