I used idea IDE to developed a Java springboot project follow a tutorial.
However, in the tutorial, there's a function shown below. I do not know where to put this function in me spring boot project.
The function:
package com.helloworld.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by fangxiao on 2017/3/24.
*/
#RestController
public class Controller {
#RequestMapping("/helloworld")
public String helloWorld() {
return "helloworld";
}
}
where should I put this function as for the below project structure?
project structure
Following your example, the Controller Class have a organization like this picture:
I also created another packages that are frequentelly used in a spring-boot project
Since your package is :
package com.helloworld.controller;
You can create folder structure inside java folder com -> helloworld -> controller and keep the code (Controller.java class) inside.
Create a package under src/main/java like com.abc.rest and create RestController class
:Here is the sample code and to access this api use url like : localhost:8080/test/headers
#RestController
#RequestMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE)
public class Rest {
#GetMapping("/headers")
public String listAllHeaders(#RequestHeader Map<String, String> headers) {
headers.forEach((key, value) -> {
System.out.println(String.format("Header '%s' = %s", key, value));
});
return "success";
}
}
For learning purpose is good to do it manually, but if you want to skip it, you can create the class shown above in the src/main/java and IntelliJ will dectect and offer to relocate it to the proper package.
Create your package com.project.controller under
D:\Bill\java\test\src\main\java
directory
Related
I'm following this simple tutorial: https://www.mkyong.com/webservices/jax-ws/jax-ws-hello-world-example-document-style/?fbclid=IwAR0vxhYrj9MKy1Q28h6luFVJoSxDP4KWBOLEu_v_Ss4uQztmB-9JuYsS4RI and at step 3 it mentions that I should receive the error:
Wrapper class com.mkyong.ws.jaxws.GetHelloWorldAsString is not found.
Have you run APT to generate them?
However, I do not get such error(no error at all) and I'm worried that it is not working as expected.
My classes:
Interface:
package com.soap3sk.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;
// Service Endpoint Interface
#WebService
#SOAPBinding(style= Style.DOCUMENT, use= Use.LITERAL) // optional
public interface NoteEndpoint {
//#WebMethod ArrayList<ToDoNote> getNotes();
#WebMethod String getHelloWorldAsString(String name);
}
Implementation:
package com.soap3sk.ws;
import javax.jws.WebService;
#WebService(endpointInterface = "com.soap3sk.ws.NoteEndpoint")
public class NoteEndpointImpl implements NoteEndpoint {
#Override
public String getHelloWorldAsString(String name) {
return "Hello World JAX-WS " + name;
}
}
Publisher:
package com.soap3sk.endpoint;
import javax.xml.ws.Endpoint;
import com.soap3sk.ws.NoteEndpointImpl;
public class NoteEndpointPublisher {
public static void main (String[] args) {
Endpoint.publish("http://localhost:5000/ws/hello", new NoteEndpointImpl());
}
}
Project structure: https://imagizer.imageshack.com/img924/3514/BAuOcl.png
What I also noticed that those 2 .class files(asString and Response that are mentioned in the guide) are not generated anywhere as well. I'm using Eclipse and created a maven project with the quickstart archetype. Runnning it as a standard java application.
I can access the wsdl file going here: http://localhost:5000/ws/hello?wsdl and the I can see getHelloWorldAsString and getHelloWorldAsStringResponse there, but they are nowhere to be seen in my project and no error is thrown that they could not be found as mentioned in the guide that it should.
I also tried downloading the sample project and deleting the .java files that should be required, but it is stil the same(no error, not asking to create those classes).
I would be very grateful if someone could help. Thank you.
EDIT
I found a similiar question here: Java web service not giving error message Could someone explain his answer? Is the creation of those two classes not necessary?
you're trying to replicate a situation reported almost 10 years ago. Don't you want to try a newer tutorial like the following:
https://www.baeldung.com/jax-ws
https://spring.io/guides/gs/producing-web-service/
I have been following this Tutorial to achieve Write Java code in nativescript and use directly in typescript
But I got an error Cannot read property 'MyToast' of undefined
app.component.ts:
import {Component} from "#angular/core";
let application = require("application");
declare var org:any;
#Component({
selector: "my-app",
templateUrl: "app.component.html",
})
export class AppComponent {
public onTap() {
org.example.MyToast.showToast(application.android.context,"You pressed the button","short");
}
}
I have create MyToast.java class in platforms-> src -> java -> org ->example :
package org.example;
import android.widget.Toast;
import android.content.Context;
public class MyToast{
public static void showToast(Context context,String text ,String StrDuration ){
Toast.makeText(context,text, Toast.LENGTH_SHORT).show();
}
}
Did you perform a build? Metadata for JavaScript->Java invocation will not be generated in a simple tns run android where a build has not been triggered.
If you touch platforms/android, a build will not be provoked, as that directory is not being watched.
tns build android - this will ensure that, when making manual changes in platforms/android, a build will be triggered.
Offtopic: Also, I am happy to share that you will be able to create those classes directly in your App_Resources/Android directory in the 4.0 release of NativeScript, without worrying for your custom class being wiped clean when changing workstations, cleaning your project.
I'm trying to create a custom SonarQube rule which will detect the usage of a specific custom Java Annotation. Here is the code I found which prints a list of all annotations used in a class.
public class SampleAnnotationCheck extends IssuableSubscriptionVisitor {
#Override
public List<Tree.Kind> nodesToVisit() {
return ImmutableList.of(Tree.Kind.METHOD);
}
#Override
public void visitNode(Tree tree) {
MethodTree methodTree = (MethodTree) tree;
for (AnnotationInstance ai : ((JavaSymbol.MethodJavaSymbol) methodTree.symbol()).metadata().annotations()) {
System.out.println(ai.symbol().name());
}
}
}
Sample Java File:
#GET
#Path(values)
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
method1(...) {...}
#CustomAnnotation(values)
#POST
#Path(values)
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
method2(...) {...}
#PATCH
#Path(values)
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
method3(...) {...}
Expected Output:
GET
Path
Consumes
Produces
CustomAnnotation
POST
Path
Consumes
Produces
PATCH
Path
Consumes
Produces
Actual Output:
GET
Path
Consumes
Produces
!unknownSymbol!
POST
Path
Consumes
Produces
!unknownSymbol!
Path
Consumes
Produces
I'm getting !unknownSymbol! instead of the Custom Annotations' actual names. One of the custom annotations is io.swagger.jaxrs.PATCH.
The other annotation is defined inside a separate package and imported by the sample class.
Do we have to register these custom annotations somewhere for the API to detect?
Please suggest what changes should be made so that we can detect and print the actual Custom Annotation's name.
Thanks in advance!
I assume you are following this guide and running check within your tests https://docs.sonarqube.org/display/PLUG/Writing+Custom+Java+Rules+101 . To provide dependencies to your tests you can either
put jars in target/test-jars directory (see how to do it with maven dependency plugin here https://github.com/SonarSource/sonar-custom-rules-examples/blob/master/java-custom-rules/pom.xml#L147)
in your test provide custom classpath using JavaCheckVerifier.verify(filename, check, classpath)
I have 2 Projects in Eclipse:
The first one is a maven project using google AdWords API dependencies and the second one is a dynamic web project. I have imported the first project by clicking on the second -> import -> Existing Maven Projects -> MyFirstProjectFolder -> Finish. It made a new pom.xml.
I've used a class from the first project in the second by importing it and running a method of it and i get an error: java.lang.NoClassDefFoundError.
I'm not a professional java programmer but as I understood the second project doesn't get the class but in my eclipse editor I have no errors or mistakes.
I hope I was precise enough. Thank you in advance.
package com.adwordsfeatures;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import url_checker.*;
#Path("/hello")
public class ServerService {
#GET
#Produces(MediaType.TEXT_PLAIN)
#Path("first")
public String hello(){
return "Hi everyone";
}
#GET
#Path("go")
public String go(){
runProgramMethod();
return "<p1>Programm running</p1>";
}
private void runProgramMethod() {
MainPauserMethod runner = new MainPauserMethod();
}
}
this is the code in the second Project and the class i'm trying to use from the first project is the MainPauserMethod (i know it's a class it's just a test i'll change the name) and it's from the package url_checker.
I cannot find an answer in the Google docs. As I understand from the docs, the #Api annotation is applied to a class to indicate that it is part of the Endpoint API and the #ApiMethod then indicates which methods of that class is part of the Cloud API.
However, even if a method is not annotated with #ApiMethod the Google App Engine Cloud Endpoints Builder still includes that method as part of the Cloud API.
How can I exclude a method from the API? If it is not possible, is a good alternative then to pass the received API parameters to a separate object (which is a field of the annotated class) that do contain the required method?
I include the following code which is a class annotated with #Api for clarification (see comment on top of ggetStr method):
package com.barcodeapp.www.app;
import java.util.ArrayList;
import java.util.List;
import com.google.api.server.spi.config.Api;
#Api(
name = "cetest",
version = "v1",
scopes = {EndpointsConstants.EMAIL_SCOPE},
clientIds = {EndpointsConstants.WEB_CLIENT_ID, EndpointsConstants.ANDROID_CLIENT_ID, com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID},
audiences = {EndpointsConstants.ANDROID_AUDIENCE}
)
public class CloudTest {
public List<String> list() {
List<String> strs = new ArrayList<String>();
strs.add("a"); strs.add("b");
return strs;
}
/* THE FOLLOWING METHOD NEEDS TO BE EXCLUDED FROM CLOUD API */
public String ggetStr() {
return "abc";
}
}
EDIT: I have included the library .jar file in the WAR/WEB-INF/lib folder - no difference.
Below is the two classes for completeness:
package com.cloudtest.lib;
public class ClassInLibProj {
}
and
package com.cloudtest.my;
public class ClassInLocalProj {
}
Thanks.
I may be wrong, but it seems that the only way currently for your method not to be included is to make that private in your class.
This is because:
If your method is annotated with APIMethod, then it is included
If you method is not annotated with APIMethod but it is public, it is taken too. This is likely because of the API annotation at the top.
There is an issue raised : https://code.google.com/p/googleappengine/issues/detail?id=10372&thanks=10372&ts=1386300958