I can't use yoccaddon in Hybris 5.5.1 - java

I am using an Hybris 5.5.1 trial for learning purposes. I want to create a ycommercewebservice addon with yoccaddon like the documentation points, but I don't see that module, just the yaddon.
Iis it the same? I followed the documentation steps but using yaddon insteado of yoccaddon.
I tried with yaddon and after following the documentation and building successfully, my new api resource throws "There is no resource for path /rest/v2/apparel-uk/testing"
My controller has:
package com.test.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
#Controller
#RequestMapping(value = "/{baseSiteId}/testing")
public class TestController
{
#RequestMapping(method = RequestMethod.GET)
#ResponseBody
public String getTesting()
{
String testText = "Hello Test";
return testText;
}
}

You need to add #ApiVersion("v2) or #ApiVersion("v1).

Hmm... I am not big familiar with the addon logic of hybris, but what I see here is that you do a request on /rest/v2/apparel-uk/testing, but you have only the following Request Mapping:
#RequestMapping(value = "/{baseSiteId}/testing")
So the part /rest/v2/ is missing.

Interesting. I've got hybris 5.5.1 here and I'm trying to find the yoccaddon myself - I can't. Makes me wonder if it's been deprecated (and if so, why the wiki documentation still refers to it...
Does the API respond if you hit /{baseSiteId}/testing ? I'd suggest adding a web module (with the relevant webfoot configured) in your extensioninfo.xml - e.g.
<webmodule jspcompile="false" webroot="/rest/v2"/>
This should then mean that your controller will be available on the expected URL.

Related

How do I address this mapping problem for Spring MVC?

package com.coding;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
#RequestMapping("/customer")
public class CustomerController {
#RequestMapping(value="/showForm")
public String showForm(Model theModel) {
theModel.addAttribute("customer",new Customer());
return "customer-form";
}
#RequestMapping("/processForm")
public String processForm
(#Valid #ModelAttribute("customer") Customer theCustomer,BindingResult theBindingResult) {
if(theBindingResult.hasErrors()) {
return"customer-form";
}else {
return "customer-confirmation";
}
}
}
New to Spring!:)
I am trying to build a dynamic project using Spring MVC which is "form validation" . I tried everything possible to solve this problem "WARNING: No mapping for GET /spring-mvc-demo/customer/showForm" but its not working . Any solution please?
As you can see this warning clearly tells the path does not exist and that it's not able to recognize at this path any handlers are available.
What you can do is, Make sure the path matches your controller method mappings.
Have you checked localhost:8080/customer/showForm? Try this URL it will work?
Hii Anjita I tried to reConstruct your problem it is working fine for me I have created a CustomerController class like this.
I created a login form like this
And I made a request from a browse with the URL "http://localhost:8080/customer/showForm". And the output is
Whenever I tried to give a URL "localhost:8080/spring-mvc-demo/customer/showForm" it is showing a Whitelabel error try removing your project name in URL and try once.

How to identify whether web service is REST or SOAP?

I am learning spring MVC and have wrote following code. I read some articles about SOAP and REST but in the beginner level controller code I have written I am not able to distinguish whether SOAP or REST is used. My controller code is as follows:
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.model.Customer;
#Controller
public class SelectController {
#RequestMapping("/")
public String display(){
System.out.println("Inside controller");
return "demo";
}
#RequestMapping("/index")
public String indexpage(HttpServletRequest req, Model m){
String name = req.getParameter("name");
String pass = req.getParameter("pass");
String random = req.getParameter("abc");
System.out.println("Index page"+name+pass+random);
Customer cust = new Customer();
cust.setUsername(name);
cust.setPassword(pass);
System.out.println("Index page"+name+pass);
m.addAttribute("customer", cust);
return "hello";
}
The Controller that you have written is
neither REST nor SOAP.
Its a MVC Controller.
In your case, your returning "hello" string at the end of controller method, which in-turn gets translated/mapped to a page(hello.jsp or hello.html based on the configuration) and returns the same. So, at the end, what you get is Page rendered in a beautiful way with all the necessary Stylings and scripts applied.
In contrast, REST and SOAP doesn't work in that way. Its main purpose is for transferring the data alone(Yet you can send HTML page also)
Writing a REST Service is almost similar to what you have currently and is fairly easy. If you use Springboot then all you have to do is just replace the #Controller annotation with #RestController and return Customer object directly. In REST Controller you wont have HttpServletRequest & Model arguments.
But writing a SOAP service is another topic which may seem entirely different.
There are tons of examples and tutorials scattered around the web, which you can find easily on these topics.
References :
Controller vs RestController in Spring
Difference between Controller & RestController in Spring
SOAP vs REST
Hope this gives some high level idea of what those three are.

delete all records in mongoDB Spring application using CURL

I'm Working with one of Spring's tutorials on integrating Spring with Mongo DB.
https://spring.io/guides/gs/accessing-mongodb-data-rest/
I simply want to be able to delete multiple records using CURL.
something like
curl -X DELETE http://localhost:8080/people/
The tutorial shows how to delete a specific record but not multiple records. Fairly new with working with CURL as well..Pretty sure I'm missing something simple, thanks.
FYI the method to delete a single record would be
curl -X DELETE http://localhost:8080/people/53149b8e3004990b1af9f229
I think, you should run this
mongo <dbname> --eval "db.people.drop()"
When you run the example project you linked, Spring Boot automatically wires a RepositoryEntityController, that exposes basically two URIs:
http://localhost:8080/people/
and
http://localhost:8080/people/{id}
where {id} is a specific person's id.
It also provides some methods and binds them with one of the URIs above. For example, there is the getCollectionResource method, that "listens to" http://localhost:8080/people/, so you can run
curl http://localhost:8080/people/
and get a list of the people saved in your database.
The method deleteItemResource on the other hand, "listens to" http://localhost:8080/people/{id}, so the specific controller does not provide the functionality of simultaneously deleting all person entities.
But you can always write your own controller and provide your custom functionality. The trivial code below will do the work (but will not include all the other methods of course):
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
#RestController
#RequestMapping("/people")
public class PersonController {
#Autowired
PersonRepository people;
#RequestMapping(method=RequestMethod.DELETE)
public void deleteAll() {
people.deleteAll();
}
}

Integrating GWT with Spring

I know that this topic was discussed many times, but I found that the most of information on this is not up to date.
I am looking for tutorial/example on how to integrate GWT with Spring framework.
I have found many examplex (some of them even working), but only with older libraries. I am looking for a solution with newest libraries (or at least compatible with the newest).
Also many examples use spring4gwt library (for creating "glue" servlet) - is there another way?
I want to create simple example application using GWT + Spring + Hibernate + Maven. I started by creating Web Application Project (from Eclipse). I converted project to Maven project. And to be honest I am stuck here. I can create simple service (+ async), but have no idea how to configure proper servlet and go further. Examples I found relay on spring4gwt, but I would like not to use it (no new version since 2009 I think).
It would be great if someone could explain integration step-by-step.
Sorry if this one is a duplicate, but after long search I haven't found clear solution that suits my needs.
You have many ways to integrate with Spring, but i think the best option is use RestyGWT Framework
Since you are using HTTP protocol and JSON format for serializing objects, you won't have problem to comunicate with the Spring Controllers using RestyGWT.
You could also use your own controllers to respond to GWT RPC Requests. Instead of using GWT Dispatcher, you use the Spring MVC Request Dispacher and map the URLS on controllers to your services in GWT client.
if you use the RESTY GWT API, you could just write your interface, map the methods using JAX-RS annotations like #POST, #GET, #DELETE, #PathParam, etc.
Here's what I'm doing on my project using RestyGWT:
The project is compose of 2 projects:
project-client
project-server
The client contains all files related to GWT and RestyGWT.
The server contains all files from the back end implementation using Spring.
Maven overlay is used to merge the 2 projects on the package compile phase, so you end with a final war with the GWT *js files and the server files.
To use RestyGWT you have to create an interface who extends RestService:
public interface MyRestService extends RestService{
#GET
#Path("/foo")
public void getFoo(MethodCallback<List<Foo>);
#POST
#Path("/foo")
public void saveFoo(Foo foo ,MethodCallback<MessageResponse>);
}
To use the service you write something like this:
MyRestService service = GWT.create(MyRestService.class);
and you will have something like this to use the service:
service.getFoo(new MethodCallBack<List<Foo>>(){
public void onSucess(List<Foo> foos){
/* You will get foos, you dont have to worry about serialization, RESTYGWT does it for you */
}
public void onError() ...
});
And you will have a controller to respond to this request like this:
#Controller
class myController{
#Autowired FooService svc;
#RequestMapping(value = "/foo", method = RequestMethod.GET, produces= "application/json")
public #ResponseBody List<Foo> getAllFoos(){
return svc.all();
}
#RequestMapping(value = "/foo", method = RequestMethod.POST, produces= "application/json", consumes="application/json")
public #ResponseBody MessageResponse save(#ResponseBody Foo foo){
svc.save(foo);
return new MessageResponse("Foo saved with sucess", 200);
}
}
I've created many projects with this setup, you don't need spring4gwt!
My solution is to use a "bridge" class that allow you to call async services like spring controllers:
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.servlet.ModelAndView;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
public abstract class BaseRemoteService extends RemoteServiceServlet implements
ServletContextAware {
private static final long serialVersionUID = 2470804603581328584L;
protected Logger logger = Logger.getLogger(getClass());
private ServletContext servletContext;
#RequestMapping(method = { RequestMethod.GET, RequestMethod.POST })
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
doPost(request, response);
return null; // response handled by GWT RPC over XmlHttpRequest
}
#Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
#Override
public ServletContext getServletContext() {
return this.servletContext;
}
}
Now, your *RpcServiceImpl should be something like:
#Controller
#RequestMapping("/*/action.service")
public class ActionRpcServiceImpl extends BaseRemoteService implements ActionRpcService {
//this class is managed by spring, so you can use #Autowired and other stuffs
//implementation of your rpc service methods,
}

Spring webservice won't read JSON

As editing a question didn't seem to bump it up for more answers, I'll continue asking in a new thread.
The thing is that I'm trying to set up a very basic web-service with Spring, to which I should be able to send JSON through Java.
But I'm very new to Spring, and web-services in general, so I don't know where to troubleshoot.
Old question: Getting a RESTful webservice with Spring to understand a JSON-string
Right now I have this in my controller
#RequestMapping(value = "/toggleB")
public #ResponseBody String sendCommand(#RequestBody Ident ident) {
//body
}
The Ident-class just has a String as variable, called IP.
Sending this String
{"IP":"192.168.1.9"}
Gives the return code 400.
Thing is, I think there might be something wrong, or missing, in the build.gradle (or pom.xml).
How is a correct Gradle/Spring project supposed to be organized as?
My main method is runnable, and looks like this:
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;
#ComponentScan
#EnableAutoConfiguration
public class mainFile {
public static void main(String[] args) {
SpringApplication.run(mainFile.class, args);
}
}
I don't use any beans as configuration, might that be what fails my program?
Thanks again for replies!
I hope I'm not breaking the rules by continuing my question like this, but I didn't know how to bump the old one up.
Ah... The disadvantages of being completely new to a site, heh.
If you're sending JSON in the request body, make it a POST or a PUT request, also add headers to accept json. Fetch the Json as a string and then use a Json parser to parse it to a JsonNode or whatever you want. And at last return a ResponseEntity. Below is a sample code. Do ask specific questions in the comment for clarification if needed.
#RequestMapping(value = "/toggleB", method = RequestMethod.POST, headers = "Accept=application/json")
public ResponseEntity<java.lang.String> sendCommand(#RequestBody String ident) {
// Do your stuff
return new ResponseEntity<String>("Some response data/message",new HttpHeaders(), HttpStatus.OK);
}

Categories